Django version 1.6 View Source Analysis About edit. py

Source: Internet
Author: User
Source: Django version 1.6 View Source Analysis About edit. py is first put on your website. Now I am here to po it out. I hope it will help you to write it in the past. I will remember to continue it later. from _ future _ import unicode_literals


From Django. Core. Exceptions import improperlyconfigured, objectdoesnotexist
From Django. DB import models
From Django. Http import http404
From Django. utils. Translation import ugettext _
From Django. Views. Generic. Base import templateresponsemixin, contextmixin, View




Class singleobjectmixin (contextmixin ):
"""
Provides the ability to retrieve a single object for further manipulation.
"""
Model = none
Queryset = none # specify it directly to serve get_object
Slug_field = 'slug' # Use queryset = queryset. Filter (** {slug_field: slug}) with get_object })
Context_object_name = none
Slug_url_kwarg = 'slug' # obtain urlconf? The transfer value of Slug = is used for PK = self. kwargs. Get (self. pk_url_kwarg, none) with get_object
Pk_url_kwarg = 'pk' # obtain urlconf? PK = Transmission Value slug = self. kwargs. Get (self. slug_url_kwarg, none) with get_object


Def get_object (self, queryset = none): # obtain the qualified OBJ condition as PK =? Or {slug_field: slug}
"""
First, determine whether queryset exists. If not, call get_queryset to assign a value to queryset.
In urlconf, PK or slug uses PK = self. kwargs. Get (self. pk_url_kwarg, none)
And slug = self. kwargs. Get (self. slug_url_kwarg, none) Get
Then, use filter () and get () to obtain obj.
Returns the object the view is displaying.


By default this requires 'self. queryset' and a 'pk' or 'slug' argument
In the urlconf, but subclasses can override this to return any object.
"""
# Use a custom queryset if provided; this is required for subclasses
# Like datedetailview
If queryset is none:
Queryset = self. get_queryset ()


# Next, try looking up by primary key.
PK = self. kwargs. Get (self. pk_url_kwarg, none)
Slug = self. kwargs. Get (self. slug_url_kwarg, none)
If PK is not none:
Queryset = queryset. Filter (PK = PK)


# Next, try looking up by slug.
Elif slug is not none:
Slug_field = self. get_slug_field ()
Queryset = queryset. Filter (** {slug_field: slug })


# If none of those are defined, it's an error.
Else:
Raise attributeerror ("generic Detail View % s must be called"
"Either an object PK or a slug ."
% Self. _ class _. _ name __)


Try:
# Get the single item from the filtered queryset
OBJ = queryset. Get ()
Couldn t objectdoesnotexist:
Raise http404 (_ ("No % (verbose_name) s found matching the query") %
{'Verbose _ name': queryset. model. _ meta. verbose_name })
Return OBJ


Def get_queryset (Self): # If queryset is null, self. model. _ default_manager.all () is returned ()
"""
Get the queryset to look an object up against. may not be called if
'Get _ object' is overridden.
"""
If self. queryset is none:
If self. Model:
Return self. model. _ default_manager.all ()
Else:
Raise improperlyconfigured ("% (CLS) S is missing a queryset. Define"
"% (CLS) S. Model, % (CLS) S. queryset, or override"
"% (CLS) S. get_queryset ()." % {
'Cls': Self. _ class _. _ name __
})
Return self. queryset. _ clone ()


Def get_slug_field (Self ):
"""
Get the name of a slug field to be used to look up by slug.
"""
Return self. slug_field


Def get_context_object_name (self, OBJ ):
"""
The returned result is self. context_object_name or obj. _ meta. model_name.
Get the name to use for the object.
"""
If self. context_object_name:
Return self. context_object_name
Elif isinstance (OBJ, models. Model ):
Return obj. _ meta. model_name
Else:
Return none


Def get_context_data (self, ** kwargs ):
"""
Result ---> Insert the single object into the context dict.
In self. object = self. call to process context ['object'] = self after get_object. object context_object_name = self. get_context_object_name (self. object) --> context [context_object_name] = self. object
Insert the single object into the context dict.
"""
Context = {}
If self. Object:
Context ['object'] = self. Object
Context_object_name = self. get_context_object_name (self. Object)
If context_object_name:
Context [context_object_name] = self. Object
Context. Update (kwargs)
Return super (singleobjectmixin, self). get_context_data (** context)




Class basedetailview (singleobjectmixin, view ):
"""
A base view for displaying a single object
"""
Def get (self, request, * ARGs, ** kwargs ):
Self. Object = self. get_object ()
Context = self. get_context_data (Object = self. Object)
Return self. render_to_response (context)




Class singleobjecttemplateresponsemixin (templateresponsemixin ):
Template_name_field = none
Template_name_suffix = '_ detail'


Def get_template_names (Self ):
"""
Return a list of template names to be used for the request. may not be
Called if render_to_response is overridden. returns the following list:


* The value of ''template _ name'' on The View (if provided)
* The contents of the ''template _ name_field ''field on
Object instance that the view is operating upon (if available)
* ''' <App_label>/<model_name> <template_name_suffix>. html''
"""
Try:
Names = super (singleobjecttemplateresponsemixin, self). get_template_names ()
Failed t improperlyconfigured:
# If template_name isn't specified, it's not a problem --
# We just start with an empty list.
Names = []


# If self. template_name_field is set, grab the value of the field
# Of that name from the object; this is the most specific template
# Name, if given.
If self. Object and self. template_name_field:
Name = getattr (self. Object, self. template_name_field, none)
If name:
Names. insert (0, name)


# The least-specific option is the default <app>/<model> _detail.html;
# Only use this if the object in question is a model.
If isinstance (self. Object, models. Model ):
Names. append ("% S/%s0000s.html" % (
Self. Object. _ meta. app_label,
Self. Object. _ meta. model_name,
Self. template_name_suffix
))
Elif hasattr (self, 'model') and self. model is not none and issubclass (self. Model, models. Model ):
Names. append ("% S/%s0000s.html" % (
Self. model. _ meta. app_label,
Self. model. _ meta. model_name,
Self. template_name_suffix
))


# If we still haven'tmanaged to find any template names, we sho'd
# Re-raise the improperlyconfigured to alert the user.
If not names:
Raise


Return names




Class detailview (singleobjecttemplateresponsemixin, basedetailview ):
"""
Render A "detail" view of an object.


By default this is a model instance looked up from 'self. queryset', but
View will support display of * any * object by overriding 'self. get_object ()'.
"""

Django version 1.6 View Source Analysis About edit. py

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.