Django Templates - Rendering to an Iterator - django

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/

Related

Display a webpage while rendering

I'm putting together a website, where one of the pages holds an interactive map. The map is implemented as a big table, where each node is a td.
Now this map takes a while to render, and so, I'd love for the site to be displayed as it renders, so that even if the map is not fully rendered, the user can click links or the part of the map that is rendered.
Is there an easy way to do this? AJAX is one option, but since it is a Django website and the map depends on data from the Django template, AJAX becomes a bit unwieldy.
So is there a way to make the page visible while rendering?
(I considered making each node an iframe, so that they would be rendered individually, but that seems a bit silly too)
the django template should only render an empty map (or a map holding the first 10 points) with the javascript code firing on page ready
this javascript script should do this:
request 10 nodes from django (using a different url/view)
render the fetched nodes into the page
if no more nodes: END
goto 1.
Hope this helps
After trying a few different things, it seems that the problem was too many database queries. Each of the nodes made calls to the database while rendering, which caused them to be very slow.
For reference:
Custom filters in Django should not make database queries, if they are used heavily on a page

Render a block through a template that will be added to an existing page as well as it's own

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?

getting the template name and the time it takes to render in a middleware based on a url

I want to write a middleware that would tell me the name of the template being rendered and the time it took, for the database queries for that particular view.
Django Debug Toolbar does the same, but due to custom request and response object written i am not able to get that working.
So i thought of writing a custom middleware that would do the same for me on a url appended with some get request variable.
Say 127.0.0.1/index/polls gives me all the polls.
If i try 127.0.0.1/index/polls/?my_tool it would invoke a url, and i would get the data.
Any suggestions on how to implement it? Is there a working example?
I could infer that you need some debug tool for development, in that case I would suggest django-debug-toolbar, otherwise, if you really need the middleware you always can look at the code of django-debug-toolbar. :)
If I´ve got to write that by myself, I would write a couple of classes (in debug toolbar are called panels) that inherits from template and timer, and override the method process_response for log or print the info that I need.
Even you can look into the middleware of debug-toolbar and picture an idea of how it works

include views vs render templates in grails

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.

where do functions that don't display go in django

I have some links on an html page like , , currently I handle them as so
<p> rate down
and have a url.py entry:
(r'^cases/(?P<case_id>\d+)/case_rate/(?P<oper>.)$', 'mysite.cases.views.case_rate'),
then I have a view function that handles the logic and hits the DB, then does this:
return HttpResponseRedirect(request.META.get('HTTP_REFERER','/'))
I's there a better way to do this? I can see how this would be OK because it does have to redraw the screen to show the new rating...
The typical way to handle this is with an ajax request.
Instead of a link, you put a javascript handler that calls a view, wich updates the db, and returns a json / xml object with the new rating for the item. Then another javascript handle receives that response and updates the rating number on the screen, without a page reload.
Ideally, you'll keep both versions: plain html (the one you currently have) and the ajax one. The ajax one can be attach to the element after page load, so if javascript is not available, you'll still have a working site.
Then, regarding organization, you can have an "ajax" parameter on your view. The view should update the db accordingly, and if it's an ajax call, return the json / xml response, otherwise, return the new page. That way you can keep the logic (fetching the object, updating the db) on one place.
If you're asking whether case_rate should still go in the views.py given that it returns a redirect rather than providing content, the answer is yes, since case_rate is handling an request and returning a response.
But consider a situation where you had two view functions in views.py that had some duplicate code, and you chose to factor that duplicate code into another function that didn't both take request and return a response. Would that be fair game to leave in views.py? Sure, if moving it elsewhere would make the code harder to read. Or you might choose to put it elsewhere. It's really your call based on your sense of taste.