What is the technical differences between render a template and include a view in grails?
You generally render a template (from a controller) as part of an AJAX request when you're updating only part of a page. I don't know what you mean by 'include a view'. If you mean render a template from a GSP then it's generally just a convenient way of reusing pieces of your presentation within a view.
So if I have a template that contains a login form, I may want to include that template in several different layouts/pages.
If I have a template that contains search results I may want to render the template from a controller in response to an AJAX request that occurs when the user scrolls to the end of my current set of results or the user updates some search criteria.
Related
What would be the correct approach to create a "stats block" which could be used easily?
I've now created a simple page template and view function which returns a list of stats e.g. users and user count by calling User.objects.all().count()
How should this be implemented to be able to include these stats in any template? And perhaps with different styles such as list, inline, bootstrap panel etc?
If you want it in any template, you probably want to write your own custom template tag. This is the relevant Django documentation: https://docs.djangoproject.com/en/2.2/howto/custom-template-tags/#writing-custom-template-tags
Your tag will probably return minimalist data: just the result of queryset.count(). The template that you are rendering will place this number in the relevant html context.
I have a page which is for album/picture management with 2 sections: Albums and Pictures.
When an album is selected the pictures block needs to change via AJAX to reflect the album selected.
This means the rendered pictures block needs to be provided to the Albums page as well as be available as it's own View for the AJAX source.
I understand I could solve this by making the pictures block always render from AJAX even when the album page loads, however I would like to deliver the default album pictures within the initial page load if possible. In order to do that, I'd like to render the pictures block via the same template in the Album page view as is used for the Picture AJAX View.
I'm only familiar with providing templates as a template_name property within an TemplateView object.
I guess I could simply call an instance of PictureView using inclusion_tag, and pull the data I need out of the render_to_response (I haven't tried this yet, I'm just theorizing) however that seems a bit dirty to me. I'm wondering if there's a more elegant solution to this problem?
Thanks!
jQuery, Django templates, JS templating and backbone.js should tie this together.
I would suggest having a dedicated template for the Pages block. Include this template in the Django template for your page.
For dynamic updates use a JS templating library such as included in underscore.js or moustache.js. You can change the template demlimiters used so that they are the same as djangos.
Include the raw Pages block template into a javascript template block - use the django ssi tag.
Use django-tastypie to set up an api that returns the data for Photos as JSON. Your template library can then use data to fill in the template in the JS template block and you can then replace the Photo block with this rendered HTML. Use of backbone.js should ease the tying of DOM elements and JS events.
If I understand your question correctly, I did something similar once with the subsection having its own template file, which only describes that one section of the page. I used the include tag to include it into the page template, so it loaded when the page did and then rendered that template with updated values and replaced it on the page with AJAX when the content was meant to change.
Is that an option for you?
I want to make a sidebar for my webapplication. It contains the following content:
A search bar
a list of tags
links to recent posts (with year and month)
I want to include this sidebar in every site/view. So my first guess is that it would belong to a layout.
But it's also dynamic and as far as i know layouts are static.
How do I avoid redundancy in my views/layouts and still have the sidebar on every site?
To have context data passed to multiple templates you have different options in django; You could either:
Make a Template Tag which can pull in the relevant data and render it and reuse it in every template you need to (or just insert it in a base template and use template inheritance).
Use a context processor: It will be called with every request and add data to every view's context that uses RequestContext.
Using Django's class based views you could have all your views inherit from a base view which adds data to your context.
If you need data from your database I would rather go with using a template tag than using a context processor as it will be called for every view.
For static content in your sidebar (e.g. search form), its straight forward template/html.
For the dynamic content like list of tags, recent posts:
Once in the template you have identified a elements (div or something else) to put this info, you can populate its content using either your custom template tag or having custom context processor.
In your case, if the content doesn't really depend upon request parameter or url, template tag would be better choice.
Reference Custom template tag Custom Context Processor
I have a django view which uses a template to display a long queryset (> 800 items). It takes several seconds for the view to render, and when it's done rendering the entire page, it sends it to the browser. Instead, I want the Template to render as an iterator, so that it can transmit the page line by line (and so I can see the page appear in my browser) as it is generated. I don't want to wait several seconds before I see anything.
Right now my view returns render_to_response('view_name.html', {items:myitems}).
Is rendering a template to an iterator as I've described possible in Django?
Not via templates, no.
You can treat an HttpResponse as an iterator, yielding your output line-by-line, but template rendering is an all-in-one process (as it has to be, because of the need to resolve blocks etc).
Your options would be to use pagination:
https://docs.djangoproject.com/en/dev/topics/pagination/
Or to use ajax. You would load your page and then request the data over an ajax request. Check out jquery.
Edit
Adding more links
Here is an example of someone using jquery and an HttpResponse with an iterator: http://forum.jquery.com/topic/ajax-partial-response
Here is an example of someone returning a chunked response from a file on the filesystem:
http://djangosnippets.org/snippets/365/
I have a file called header.html and it is included by base.html. In header.html, I have a list of Categories, which are stored in the database. Now, I want to give that list to header.html. The problem is that no function is known to render the data into this file. So how do I do now. Heartfelt thanks!
You need a template tag - specifically, an inclusion tag. This will render a template with a custom context, in your case the list of categories.
If you want to make variables available in all templates without specifically passing them from a view you can use a Template Context Processor
That will populate your RequestContext, available in the template.
I also suggest you look at Template Inheritence to build the relationships between templates instead of including one within the other.