当然你也可用另一个:publisher_list,如你所想,publisher是模型的名字,这是Django为你提供的第二选择。如果你仍然觉得这个名字不完美,你也可以自己命名,方法是在通用视图中添加context_object_name变量: 1 2 3 4 5 6 7 # views.py fromdjango.views.genericimportListView frombooks.modelsimportPublisher classPubli...
在这个视图中,使用定制的form_class并重写form_valid()来增加user: 1 2 3 4 5 6 7 8 9 10 11 12 # views.py fromdjango.views.generic.editimportCreateView frommyapp.modelsimportAuthor frommyapp.formsimportAuthorForm classAuthorCreate(CreateView): form_class=AuthorForm model=Author defform_valid(...
from django.views.generic import DetailView from books.models import Book, Publisher class PublisherDetailView(DetailView): model = Publisher def get_context_data(self, **kwargs): # Call the base implementation first to get a context context = super().get_context_data(**kwargs) # Add in a...
Classy Class-Based Viewsprovides a nice interface to navigate the class hierarchy of the built-in class-based views. Simple generic views¶ View¶ classView¶ Attributes(with optional accessor): http_method_names Methods as_view() dispatch() ...
class-based views的优点之一:允许编写可重用的行为,REST Framework 通过提供许多预先构建的视图来利用这一点,这些视图提供了常用的模式。 REST framework 提供的通用视图使您可以快速构建与数据库模型紧密映射的API视图。 如果通用视图不适合您的API需求,则可以使用常规APIView,或重用通用视图使用的mixin和基类来组成自己...
https://docs.djangoproject.com/en/3.1/topics/http/urls/ Examples: Function views 1. Add an import: from my_app import views 2. Add a URL to urlpatterns: path('', views.home, name='home') Class-based views 1. Add an import: from other_app.views import Home ...
1. Generic views通用视图 1.1 通用视图介绍 Django REST framework的Generic views称为通用视图,日常使用较多。基于类的视图的主要优势之一是它们允许您组合可重用的行为。DRF利用了这一点,提供了许多预构建的视图。Generic views通用视图相关类如下: generics模块 ...
only the View class deals with the fundamental interaction of python classes with the Django request/response flow and infrastructure - while the other classes are just "one way of doing it" even thought those in base.py are pretty hard to argue with.comment...
>—[Django Documentation][cite] One of the key benefits of class-based views is the way they allow you to compose bits of reusable behavior. REST framework takes advantage of this by providing a number of pre-built views that provide for commonly used patterns. ...
(In[14808]) Fixed#14803-- Corrected an inconsistency in redirection handling between old-style generic views and class-based views. Thanks to gg for the report and patch. comment:4byJacob,13年 ago milestone:1.3 Milestone 1.3 deleted Note:SeeTracTicketsfor help on using tickets. ...