http://blog.csdn.net/thinkinside/article/details/7251130
If you are not familiar with the basics of Django, please refer to the "Django in Action" series.
Welcome to the Django column: http://blog.csdn.net/column/details/django.html
Executive Summary
1. Reviewing Django's view functions
2. Using templates in view functions
3. Two tools for simplifying view functions
4. Using generic view functions
5. Generic views provided by Django
1. Review Django's view functions
....................................................................
2. Use templates in view functions
The response object returned by the view function contains some header (Header) information and content (Content), and we usually generate content through templates, so the view function we often use should be like this:
3. Two tools to simplify view functions
It's really tiresome to always write code like this, so Django provides us with two useful tools:
one is the django.shortcuts.render_to_response function, which accepts a series of parameters, including the template path, the dictionary used by the context, the original The context instance and the mimetype to be set, etc.:
render_to_response(template_path_name, dictionary=None, context_instance=None, mimetype=None)
The other is the locals() function, which assembles all local variables into a dictionary.
With these two tools, the view function can be written like this:
4. Use generic view functions
With these two tools, writing view functions really simplifies a lot. But are you satisfied with this? Django doesn't stop there, and further notices that many view functions are actually doing the same thing, such as displaying a list of a set of model objects, displaying detailed information about model objects, adding, deleting, and modifying model objects. In order to simplify the handling of these cases, Django defines a series of generic views (generic views), we only need to use these built-in generic view functions, without writing our own to achieve the corresponding functions.
The way to use a generic view is to create a parameter dictionary in the URLconf file, and use the dictionary as the third member of the URLconf tuple to automatically pass parameters to it when calling the view function. For example, the object_list function in the django.views.generic.list_detail module is used to display a list of model objects, and can accept the queryset parameter as the result set of model objects. If we want to use this view function to display the list of the first model class Product created in the "Django in Action" series, it can be configured in the URLconf like this:
In addition to queryset, the object_list function can accept many other parameters:
- template_name=
- context_processors=
After the generic view function receives the parameter, it will create the context, render the template, and finally return the HTTPResponse object. The context created by the object_list function will contain the following dictionary items:
object_list List of objects to be displayed
is_paginated Whether paging results_per_page
If paging, store the number of records per page
has_next Whether there is a next page
has_previous Whether there is a previous
page page Current page number
next Next page
previous Previous page
pages Total number of pages
hits Total number of entries
last_on_page The ordinal number of the last record on this page (starting from 1)
first_on_page The ordinal number of the first record on this page (starting from 1)
page_range A list of page ranges
If these context dictionary items do not meet your needs, you can also pass in a dictionary by specifying the extra_context parameter, and the contents of the dictionary will be merged into the context dictionary.
The context dictionary item will be used by the template. If no template is specified, the function will use [app_name]/[model_name]_list.html as the template, and the method of specifying the template is also through the parameter dictionary, for example:
In addition to object_list, Django provides a number of generic view functions, spread across several modules:
django.views.generic.list_detail模块
- object_list Displays a list of model objects
- object_detail displays a single model object
django.views.generic.create_update模块
- create_object creates a model object
- update_object modifies the model object
- delete_object deletes a model object
django.views.generic.simple module
- direct_to_template renders the given context object directly using the specified template
- redirect_to redirects to the specified url
django.views.generic.date_based模块
This module mainly deals with the function of "viewing archives by time", which comes from the news and publishing industry. Specifically include:
- archive_index top-level archive, listing all years and the specified number of newest objects
- archive_year archives by year, listing all months that own the object
- archive_month archives by month, lists all objects of this month, finds the previous, next month that owns the object
- archive_week archives by week, listing all objects for the week
- archive_day archives by day, lists all objects of the day, finds the previous, next date of the owned object
- archive_today Archive by day for the current date (today)
- object_detail Displays objects found by year/month/day/serial number
These general view functions will not be introduced one by one. You can refer to the Django API documentation, pay attention to their parameters, context content and default templates, and then you can basically master their use.
Detailed page: http://www.verydemo.com/demo_c122_i4023.html