If you find you’re struggling to implement your view as a subclass of a generic view, then you may find it more effective to write just the code you need, using your own class-based or functional views. More examples of generic views are available in some third party applications, or ...
from django.http import HttpResponse class BaseView(HttpRespones): def __init__(self, request, *args, **kwargs): content = self.get(*args, **kwargs) super(MyView, self).__init__(content) def get(self): return 'Hello, world!' ...
如果我们不指明的话,Django将会自己推断使用哪个模板,在这个例子中,Django将会推断要使用“books/publisher_list.html”,其中“books”是定义模型的app的名字,“publisher”是模型名字的小写。 注意:自动寻找模板的功能仅在django.template.loaders.app_diretories.Loader被启用的情况下可行(在TEMPLATE_LOADERS中设置) 这...
从Django 1.9开始,您可以method_decorator直接在类上使用: fromdjango.utils.decoratorsimportmethod_decorator @method_decorator(csrf_exempt, name='dispatch')classChromeLoginView(View):defget(self, request):returnJsonResponse({'status': request.user.is_authenticated()})defpost(self, request): username= req...
classArchiveIndexView[source]¶ A top-level index page showing the “latest” objects, by date. Objects with a date in thefutureare not included unless you setallow_futuretoTrue. Ancestors (MRO) django.views.generic.list.MultipleObjectTemplateResponseMixin ...
However, the base class, View, is located in the generic module, implying that it is somehow tied to generic views. I propose that this base class is a good default for anyone doing class-based views in Django, and that its use outside the context of generic views should be made explic...
classMultipleFieldLookupMixin:"""Apply this mixin to any view or viewset to get multiple field filteringbased on a `lookup_fields` attribute, instead of the default single field filtering."""defget_object(self):queryset=self.get_queryset()# Get the base querysetqueryset=self.filter_queryset...
The classUpdateViewbehaves in an identical fashion toCreateView. The only difference is that it automatically loads an item based on thepkparameter. Django uses this convention for the primary key for an item. Python from.importmodelsfromdjango.viewsimportgenericclassDogUpdateView(generic.CreateView)...
Here is smarter_views.py in your app:from smarter import site, GenericViews from models import Model class Views(GenericViews): model = Model # ... site.register(Views)... And urls.py:from django.conf.urls import patterns, include, url import smarter smarter.autodiscover() urlpatterns = ...
django-bracesprovides useful Mixins for Django's class-based views. Most of these mixins replicate the behavior of Django's function-based view decorators. Others solve common headaches with working with class-based views. You can read more inthe documentation. ...