Methods

@classonlymethoddefas_view(cls,**initkwargs):""" Main entry point for a request-response process. """# sanitize keyword argumentsforkeyininitkwargs:ifkeyincls.http_method_names:raiseTypeError(u"You tried to pass in the %s method name as a "u"keyword argument to %s(). Don't do that."%(key,cls.__name__))ifnothasattr(cls,key):raiseTypeError(u"%s() received an invalid keyword %r"%(cls.__name__,key))defview(request,*args,**kwargs):self=cls(**initkwargs)returnself.dispatch(request,*args,**kwargs)# take name and docstring from classupdate_wrapper(view,cls,updated=())# and possible attributes set by decorators# like csrf_exempt from dispatchupdate_wrapper(view,cls.dispatch,assigned=())returnview

defdispatch(self,request,*args,**kwargs):# Try to dispatch to the right method; if a method doesn't exist,# defer to the error handler. Also defer to the error handler if the# request method isn't on the approved list.ifrequest.method.lower()inself.http_method_names:handler=getattr(self,request.method.lower(),self.http_method_not_allowed)else:handler=self.http_method_not_allowedself.request=requestself.args=argsself.kwargs=kwargsreturnhandler(request,*args,**kwargs)

defget_context_object_name(self,obj):""" Get the name to use for the object. """ifself.context_object_name:returnself.context_object_nameelifhasattr(obj,'_meta'):returnsmart_str(obj._meta.object_name.lower())else:returnNone

defget_object(self,queryset=None):""" 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 DateDetailViewifquerysetisNone:queryset=self.get_queryset()# Next, try looking up by primary key.pk=self.kwargs.get('pk',None)slug=self.kwargs.get('slug',None)ifpkisnotNone:queryset=queryset.filter(pk=pk)# Next, try looking up by slug.elifslugisnotNone:slug_field=self.get_slug_field()queryset=queryset.filter(**{slug_field:slug})# If none of those are defined, it's an error.else:raiseAttributeError(u"Generic detail view %s must be called with "u"either an object pk or a slug."%self.__class__.__name__)try:obj=queryset.get()exceptObjectDoesNotExist:raiseHttp404(_(u"No %(verbose_name)s found matching the query")%{'verbose_name':queryset.model._meta.verbose_name})returnobj

Get the queryset to look an object up against. May not be called if
`get_object` is overridden.

55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

defget_queryset(self):""" Get the queryset to look an object up against. May not be called if `get_object` is overridden. """ifself.querysetisNone:ifself.model:returnself.model._default_manager.all()else:raiseImproperlyConfigured(u"%(cls)s is missing a queryset. Define "u"%(cls)s.model, %(cls)s.queryset, or override "u"%(cls)s.get_object()."%{'cls':self.__class__.__name__})returnself.queryset._clone()

Constructor. Called in the URLconf; can contain helpful extra
keyword arguments, and other things.

20
21
22
23
24
25
26
27
28

def__init__(self,**kwargs):""" Constructor. Called in the URLconf; can contain helpful extra keyword arguments, and other things. """# Go through keyword arguments, and either save their values to our# instance, or raise an error.forkey,valueinkwargs.iteritems():setattr(self,key,value)