Django field sorts Class View

字段排序
{% for band in bands %}
<a href="/show_player/?id={{ band.id }}">{{ band.name }}:{{ band.player_set.all|length }}</a>
{% endfor %}

 

 

Class View

from django.views import View

# 类视图获取列表及详情
class Categoodlist(View):
def get(self,request):
cate=Cate.objects.all()
data={'cate':cate}
return render(request,'web/index.html',data)
def post(self,request):
return HttpResponse('post')
def put(self,request):
return HttpResponse('put')
def delete(self,request):
return HttpResponse('delete')


urls.py:
from django.views.generic import TemplateView # Class View

Class view of the advantages:
readability good class view relative to the view function has a higher reusability, if other places need to use a specific method of a class, the direct successor of the class will be able to view
class view using the definition of class view view need to inherit from the parent class provided by Django
introduction method:
from django.views.generic import view
or: when routing configuration from django.views.generic.base import view, requires the use of class as_view view ( ) method to register add

 

Django- routes specified template
view, TemplateView, RedirectView three class implements most of the functionality to create a Django class view. They can be considered the father of view, they can be used alone, can inherit from it. They may not provide all the functionality required for the project, in this case, you can use views Mixins and Generic class-based.
Many Django's built inherit from view, or a variety of other mixin class based on the view class. Because of this inheritance chain is very important, so the ancestor class recorded under the heading ancestor (MRO) of. Initials MRO Method Resolution Order acronym.
A, View django.views.generic.base.View based on the basic view of the main class. All other class-based views are inherited from the base class. General view on it is not a strict sense, it is also possible to import from django.views.
The method of flowchart
dispatch ()
http_method_not_allowed ()
Options ()

Properties: list of HTTP method name http_method_names will accept this view. Default: [ 'get', 'post ', 'put', 'patch', 'delete', 'head', 'options', 'trace']
Method: classmethodas_view (** initkwargs) returns a callable view, the view receives the request and returns the response:
response = MyView.as_view () (request)
returns view and having view_class view_initkwargs properties.

Example views.py:

from django.http import HttpResponse from django.views import View
class MyView(View):
def get(self, request, *args, **kwargs):
return HttpResponse('Hello, World!')

Example the urls.py:
from django.urls Import path
from the MyView Import myapp.views
the urlpatterns = [path ( 'Mine /', MyView.as_view (), name = 'My-View'),] in the call request / response cycle when HttpRequest view, request will be assigned to the view property. URL patterns captured from any location and / or keywords and parameters are assigned to args kwargs properties. Then dispatch () called.
dispatch (request, args, * kwargs) view view of a portion of - the parameters and method of receiving the request, and returns an HTTP response.
The default implementation will check the HTTP method and the method attempts to delegate given HTTP matching method; a GET to be delegated to get () a, POSTto post ( ), and the like.
By default, HEAD request to delegate a get (). If you need to request a different manner HEAD GET, you can cover the head () method. For an example, see support for other HTTP methods.
http_method_not_allowed (request, args, * kwargs) If you are using does not support the HTTP method calls view, this method will be called.
The default implementation returns HttpResponseNotAllowed plain text list of allowed methods.
options (request, args, * kwargs) in response to the processing request handler OPTIONS HTTP verbs. Allow view allows a response returns an HTTP method name list header.
Two, TemplateView django.views.generic.base.TemplateView presentation given template, which contains the context parameters captured in the URL.
This view inherits view class
django.views.generic.base.TemplateResponseMixin
django.views.generic.base.ContextMixin
django.views.generic.base.View

方法流程图
dispatch() http_method_not_allowed() get_context_data()
实例:views.py
from django.views.generic.base import TemplateView
from articles.models import Article
class HomePageView(TemplateView):
template_name = "home.html"

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['latest_articles'] = Article.objects.all()[:5]
return context

Example the urls.py:
from django.urls Import path
from myapp.views Import HomePageView
the urlpatterns = [path ( '', HomePageView.as_view (), name = 'Home'),] three, RedirectView django.views.generic.base. RedirectView redirected to the given URL.
String format given URL may contain dictionary-style, will be interpolated according to the parameters captured in the URL. Because keyword interpolation is always done (even if no incoming parameters),
if a given URL is None, Django will return a HttpResponseGone (410).
This view of the view class inherits
django.views.generic.base.View flowchart of a method
dispatch ()
http_method_not_allowed ()
get_redirect_url ()

示例views.py:
from django.shortcuts import get_object_or_404 from django.views.generic.base import RedirectView
from articles.models import Article
class ArticleCounterRedirectView(RedirectView):
permanent = False
query_string = True
pattern_name = 'article-detail'

def get_redirect_url(self, *args, **kwargs):
article = get_object_or_404(Article, pk=kwargs['pk'])
article.update_counter()
return super().get_redirect_url(*args, **kwargs)

Example the urls.py:
from django.urls django.views.generic.base Import Import path from the RedirectView
from article.views Import ArticleCounterRedirectView, ArticleDetail
the urlpatterns = [path ( 'counter //', ArticleCounterRedirectView.as_view (), name = 'Article This article was -counter '), path (' details // ', ArticleDetail.as_view (), name =' article-detail '), path (' go-to-django / ', RedirectView.as_view (url =' https: // djangoproject.com '), name =' go -to-django '),] property
url to redirect to a string as a string. None if it is triggered 410 (Gone) HTTP error.
pattern_name name to the URL patterns you want to redirect. Use this view with the same args and kwargs incoming will complete reversal.
permanent redirect whether it should be permanent. The only difference here is the HTTP status code is returned. If True, the redirect will use status code 301. If False, the redirector 302 uses the status code. By default, permanent is False.
whether query_string GET query string is passed to the new location. If True, the query string appended to the URL. If False, then discard the query string. By default query_string is False.
method
get_redirect_url (args, * kwargs) structure redirect target URL.
The default implementation url used as starting string, and using the extended URL% named group captured in performing the named parameter string.
If the url is not set, get_redirect_url ((set using named and unnamed) attempt to reverse pattern_name content captured using the URL).
If the request query_string, it will query string appended to the URL generated. Subclasses can implement any behavior they wish, as long as the method returns a URL string can be redirected.

 

Guess you like

Origin www.cnblogs.com/wyf2019/p/10959536.html