How to process Context in a common view of Django

Source: Internet
Author: User
This article mainly introduces how to process Context in the common view of Django. Django is the most popular Pythonweb development framework. For more information, see Create a friendly template Context

You may have noticed that the publisher list template in the example saves all the books in the variable object_list. This method works well, but it is not very friendly to the people who write the template. They must know that the book is being processed here. The better variable name should be publisher_list, so that the content represented by the variable is obvious.

We can easily modify the name of the template_object_name parameter as follows:

from django.conf.urls.defaults import *from django.views.generic import list_detailfrom mysite.books.models import Publisherpublisher_info = {  'queryset': Publisher.objects.all(),  'template_name': 'publisher_list_page.html',  'template_object_name': 'publisher',}urlpatterns = patterns('',  (r'^publishers/$', list_detail.object_list, publisher_info))

In the template, the general view creates a variable name that represents the list item by appending a _ list after the template_object_name.

It is always a good idea to use useful template_object_name. The partner of your design template will thank you.
Add additional Context

You often need to present more additional information than general views. For example, consider displaying a list of all other publishers on the details page of each publisher. The object_detail General View provides publisher information for context, but it seems that there is no way to obtain the list of all publishers in the template.

This is the solution: all common views have an additional optional parameter extra_context. This parameter is a dictionary data type that contains additional objects to be added to the context of the template. Therefore, to provide a list of all publishers to the view, we use the info dictionary as follows:

publisher_info = {  'queryset': Publisher.objects.all(),  'template_object_name': 'publisher',  **'extra_context': {'book_list': Book.objects.all()}**}

In this way, a variable {book_list} is put into the context of the template. This method can be used to transmit arbitrary data to a common view template, which is very convenient. This is very convenient

However, there is a very hidden BUG here. do you know you have found it?

Let's take a look at the problem that extra_context contains database queries. In this example, we put Publisher. objects. all () in URLconf and it will only be executed once (when URLconf is loaded for the first time ). When you add or delete publishers, you will find that before restarting the Web server, general views do not reflect these changes (for more information about when QuerySet is cached and assigned values, see "cache and query set" in Appendix C ).

Remarks

This problem does not apply to the queryset parameter of the general view. Because Django knows that some special querysets can never be cached, the general view performs cache cleanup before rendering.

The solution to this problem is to use a callback in extra_context instead of a variable. Any Callable object (such as a function) that is passed to extra_context will be executed before each view rendering (rather than only once ). You can define a function as follows:

**def get_books():**  **return Book.objects.all()**publisher_info = {  'queryset': Publisher.objects.all(),  'template_object_name': 'publisher',  'extra_context': **{'book_list': get_books}**}

Alternatively, you can use another method that is not so clear but short. In fact, Publisher. objects. all itself can be called:

publisher_info = {  'queryset': Publisher.objects.all(),  'template_object_name': 'publisher',  'extra_context': **{'book_list': Book.objects.all}**}

Note that Book. objects. all is not followed by parentheses. This indicates that this is a reference to a function and it is not actually called (a general view will call it during rendering ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.