from django.views.generic.simple import direct_to_template
urlpatterns = patterns('',
(r'^foo/$', direct_to_template, {'template': 'foo_index.html'}),
(r'^foo/(?P<id>\d+)/$', direct_to_template, {'template': 'foo_detail.html'}),
)
from django.views.generic.simple import redirect_to
urlpatterns = patterns('',
('^foo/(?P<id>\d+)/$', redirect_to, {'url': '/bar/%(id)s/'}),
)
from django.views.generic.simple import redirect_to
urlpatterns = patterns('',
('^foo/(?P<id>\d+)/$', redirect_to, {'url': '/bar/%(id)s/', 'permanent': False}),
)
from django.views.generic.simple import redirect_to
urlpatterns = patterns('',
('^bar/$', redirect_to, {'url': None}),
)
from django.views.generic.simple import redirect_to
urlpatterns = patterns('',
('^bar/$', redirect_to, {'url': '%%7Ejacob.'}),
)
date_list: A DateQuerySet object containing all years that have have objects available according to queryset, represented as datetime.datetime objects. These are ordered in reverse. This is equivalent to queryset.dates(date_field, 'year')[::-1].
latest: The num_latest objects in the system, ordered descending by date_field. For example, if num_latest is 10, then latest will be a list of the latest 10 objects in queryset.
This variable’s name depends on the template_object_name parameter, which is 'latest' by default. If template_object_name is 'foo', this variable’s name will be foo.
date_list: A DateQuerySet object containing all months that have have objects available according to queryset, represented as datetime.datetime objects, in ascending order.
year: The given year, as a four-character string.
object_list: If the make_object_list parameter is True, this will be set to a list of objects available for the given year, ordered by the date field. This variable’s name depends on the template_object_name parameter, which is 'object' by default. If template_object_name is 'foo', this variable’s name will be foo_list.
If make_object_list is False, object_list will be passed to the template as an empty list.
year: The object’s four-digit year (a string).
month: The object’s month , formatted according to the month_format argument.
day: The object’s day , formatted according to the day_format argument.
queryset: A QuerySet that contains the object.
date_field: The name of the DateField or DateTimeField in the QuerySet‘s model that the generic view should use to look up the object according to year, month and day.
Either object_id or (slug and slug_field) is required.
If you provide object_id, it should be the value of the primary-key field for the object being displayed on this page.
Otherwise, slug should be the slug of the given object, and slug_field should be the name of the slug field in the QuerySet‘s model. By default, slug_field is 'slug'.
month_format: A format string that regulates what format the month parameter uses. This should be in the syntax accepted by Python’s strftime(). It’s set to "%b" by default, which is a three-letter month abbreviation. To change it to use numbers, use "%m".
day_format: Like month_format, but for the day parameter. It defaults to "%d" (day of the month as a decimal number, 01-31).
template_name: The full name of a template to use in rendering the page. This lets you override the default template name (see below).
template_name_field: The name of a field on the object whose value is the template name to use. This lets you store template names in the data. In other words, if your object has a field 'the_template' that contains a string 'foo.html', and you set template_name_field to 'the_template', then the generic view for this object will use the template 'foo.html'.
It’s a bit of a brain-bender, but it’s useful in some cases.
template_loader: The template loader to use when loading the template. By default, it’s django.template.loader.
extra_context: A dictionary of values to add to the template context. By default, this is an empty dictionary. If a value in the dictionary is callable, the generic view will call it just before rendering the template.
context_processors: A list of template-context processors to apply to the view’s template.
template_object_name: Designates the name of the template variable to use in the template context. By default, this is 'object'.
mimetype: The MIME type to use for the resulting document. Defaults to the value of the DEFAULT_CONTENT_TYPE setting.
allow_future: A boolean specifying whether to include “future” objects on this page, where “future” means objects in which the field specified in date_field is greater than the current date/time. By default, this is False.
Use the page parameter in the URLconf. For example, this is what your URLconf might look like:
(r'^objects/page(?P<page>[0-9]+)/$', 'object_list', dict(info_dict))
Pass the page number via the page query-string parameter. For example, a URL would look like this:
/objects/?page=3
To loop over all the available page numbers, use the page_range variable. You can iterate over the list provided by page_range to create a link to every page of results.
/objects/?page=last
queryset: A QuerySet that contains the object.
Either object_id or (slug and slug_field) is required.
If you provide object_id, it should be the value of the primary-key field for the object being displayed on this page.
Otherwise, slug should be the slug of the given object, and slug_field should be the name of the slug field in the QuerySet‘s model. By default, slug_field is 'slug'.
template_name: The full name of a template to use in rendering the page. This lets you override the default template name (see below).
template_name_field: The name of a field on the object whose value is the template name to use. This lets you store template names in the data. In other words, if your object has a field 'the_template' that contains a string 'foo.html', and you set template_name_field to 'the_template', then the generic view for this object will use the template 'foo.html'.
It’s a bit of a brain-bender, but it’s useful in some cases.
template_loader: The template loader to use when loading the template. By default, it’s django.template.loader.
extra_context: A dictionary of values to add to the template context. By default, this is an empty dictionary. If a value in the dictionary is callable, the generic view will call it just before rendering the template.
context_processors: A list of template-context processors to apply to the view’s template.
template_object_name: Designates the name of the template variable to use in the template context. By default, this is 'object'.
mimetype: The MIME type to use for the resulting document. Defaults to the value of the DEFAULT_CONTENT_TYPE setting.
Either form_class or model is required.
If you provide form_class, it should be a django.forms.ModelForm subclass. Use this argument when you need to customize the model’s form. See the ModelForm docs for more information.
Otherwise, model should be a Django model class and the form used will be a standard ModelForm for model.
post_save_redirect: A URL to which the view will redirect after saving the object. By default, it’s object.get_absolute_url().
post_save_redirect may contain dictionary string formatting, which will be interpolated against the object’s field attributes. For example, you could use post_save_redirect="/polls/%(slug)s/".
login_required: A boolean that designates whether a user must be logged in, in order to see the page and save changes. This hooks into the Django authentication system. By default, this is False.
If this is True, and a non-logged-in user attempts to visit this page or save the form, Django will redirect the request to /accounts/login/.
template_name: The full name of a template to use in rendering the page. This lets you override the default template name (see below).
template_loader: The template loader to use when loading the template. By default, it’s django.template.loader.
extra_context: A dictionary of values to add to the template context. By default, this is an empty dictionary. If a value in the dictionary is callable, the generic view will call it just before rendering the template.
context_processors: A list of template-context processors to apply to the view’s template.
form: A django.forms.ModelForm instance representing the form for creating the object. This lets you refer to form fields easily in the template system.
For example, if the model has two fields, name and address:
<form action="" method="post">
<p>{{ form.name.label_tag }} {{ form.name }}</p>
<p>{{ form.address.label_tag }} {{ form.address }}</p>
</form>
See the forms documentation for more information about using Form objects in templates.
Either form_class or model is required.
If you provide form_class, it should be a django.forms.ModelForm subclass. Use this argument when you need to customize the model’s form. See the ModelForm docs for more information.
Otherwise, model should be a Django model class and the form used will be a standard ModelForm for model.
Either object_id or (slug and slug_field) is required.
If you provide object_id, it should be the value of the primary-key field for the object being displayed on this page.
Otherwise, slug should be the slug of the given object, and slug_field should be the name of the slug field in the QuerySet‘s model. By default, slug_field is 'slug'.
post_save_redirect: A URL to which the view will redirect after saving the object. By default, it’s object.get_absolute_url().
post_save_redirect may contain dictionary string formatting, which will be interpolated against the object’s field attributes. For example, you could use post_save_redirect="/polls/%(slug)s/".
login_required: A boolean that designates whether a user must be logged in, in order to see the page and save changes. This hooks into the Django authentication system. By default, this is False.
If this is True, and a non-logged-in user attempts to visit this page or save the form, Django will redirect to LOGIN_URL (which defaults to /accounts/login/).
template_name: The full name of a template to use in rendering the page. This lets you override the default template name (see below).
template_loader: The template loader to use when loading the template. By default, it’s django.template.loader.
extra_context: A dictionary of values to add to the template context. By default, this is an empty dictionary. If a value in the dictionary is callable, the generic view will call it just before rendering the template.
context_processors: A list of template-context processors to apply to the view’s template.
template_object_name: Designates the name of the template variable to use in the template context. By default, this is 'object'.
form: A django.forms.ModelForm instance representing the form for editing the object. This lets you refer to form fields easily in the template system.
For example, if the model has two fields, name and address:
<form action="" method="post">
<p>{{ form.name.label_tag }} {{ form.name }}</p>
<p>{{ form.address.label_tag }} {{ form.address }}</p>
</form>
See the forms documentation for more information about using Form objects in templates.
object: The original object being edited. This variable’s name depends on the template_object_name parameter, which is 'object' by default. If template_object_name is 'foo', this variable’s name will be foo.
model: The Django model class of the object that the form will delete.
Either object_id or (slug and slug_field) is required.
If you provide object_id, it should be the value of the primary-key field for the object being displayed on this page.
Otherwise, slug should be the slug of the given object, and slug_field should be the name of the slug field in the QuerySet‘s model. By default, slug_field is 'slug'.
post_delete_redirect: A URL to which the view will redirect after deleting the object.
login_required: A boolean that designates whether a user must be logged in, in order to see the page and save changes. This hooks into the Django authentication system. By default, this is False.
If this is True, and a non-logged-in user attempts to visit this page or save the form, Django will redirect to LOGIN_URL (which defaults to /accounts/login/).
template_name: The full name of a template to use in rendering the page. This lets you override the default template name (see below).
template_loader: The template loader to use when loading the template. By default, it’s django.template.loader.
extra_context: A dictionary of values to add to the template context. By default, this is an empty dictionary. If a value in the dictionary is callable, the generic view will call it just before rendering the template.
context_processors: A list of template-context processors to apply to the view’s template.
template_object_name: Designates the name of the template variable to use in the template context. By default, this is 'object'.
Aug 21, 2013
Примечание
From Django 1.3, function-based generic views have been deprecated in favor of a class-based approach, described in the class-based views topic guide and detailed reference.