未加星标

Django - migrating from function based views to class based views

字体大小 | |
[开发(python) 所属分类 开发(python) | 发布者 店小二04 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏

The single most significant advantage in Django class-based views is inheritance. On a large project, it's likely that we will have lots of similar views. Instead of writing the repeated code we can simply have our views inherit from a base view. Also,Django ships with a collection of generic view classes that can be used to do some of the most common tasks.

1. Template View Function based view urls.py

from django.conf.urls import url

from . import views

urlpatterns = [

url(r'^about-us/$', views. about_us , name=" about _us"),

]

views.py

from django.shortcuts import render

def about_us(request):

return render(request, 'templates/contact.html')

Class based view

urls.py

from django.conf.urls import url

from .views import AboutUs

urlpatterns = [

url(r'^about-us/$', AboutUs.as_view(), name="about_us"),

]

views.py

from django.views.generic import TemplateView

class AboutUs (TemplateView):

template_name = "templates/about.html"

or we can directly write it in "urls.py" urls.py

from django.conf.urls import url

from django.views.generic import TemplateView

urlpatterns = [

url(r'^about-us/$', TemplateView.as_view( template_name= "templates/about.html"), name="about_us"),

]

2. Detail View Function based view urls.py

from django.conf.urls import url

from . import views

urlpatterns = [ url(r'^author / (?P<pk>[0-9]+)/details/$', views.author_details, name=" author_details "),

]

views.py

from django.shortcuts import render

from django.shortcuts import get_object_or_404

from books.models import Author

def author_details (request, pk):

author = get_object_or_404( Author, id=pk)

context = {'author': author}

return render(request,

context,

'templates/author_details.html')

Class based view

urls.py

from django.conf.urls import url

from .views import AuthorDetails

urlpatterns = [ url(r'^author / (?P<pk>[0-9]+)/details/$', AuthorDetails .as_view() , name="author_details "),

]

views.py

from django.views.generic import DetailView

from books.models import Author

class AuthorDetails(DetailView):

model = Author

slug_field = 'pk' # for identifying the object uniquely

context_object_name = 'author' # this name is used access the object inside template (it's optional)

template_name = "templates/

author_details

.html"

we can access object in template as "object" or model name with lowercase letters "modelname" (ex: object.name or author.name )

Note : To send extra context data to the template we can override the super class method get_context_data

from django.views.generic import DetailView

from .models import Author, Book

class AuthorDetails(DetailView):

model = Author

slug_field = 'pk'

template_name = "templates/ author_details .html"

def get_context_data(self, **kwargs):

context = super( AuthorDetails , self).get_context_data(**kwargs)

context['book_list'] = Book.objects.filter(author=self.get_object())

return context

3. List View Function based view urls.py

from django.conf.urls import url

from . import views

urlpatterns = [

url(r'^authors-list/$', views.authors_list, name=" authors_list "),

]

views.py

from django.shortcuts import render

from books.models import Author

def authors_list (request):

authors_list = Author.objects.all()

context = {' authors_list' : authors_list }

return render(request,

context,

'templates/

authors_list

.html')

Class based view

urls.py

from django.conf.urls import url

from .views import AuthorDetails

urlpatterns = [

url(r'^authors-list/$', AuthorsList .as_view() , name=" authors_list "),

]

views.py

from django.views.generic import ListView

from books.models import Author

class AuthorsList(ListView):

model = Author

template_name = "templates/

authors_list

.html"

we can access objects in template as "object_list" or model name with lowercase letters "modelname_list"

List view with dynamic filtering

urls.py

from django.conf.urls import url

from . import views

urlpatterns = [ url(r'^authors-list/subject/(?P<category>[-\w]+)/$', views.authors_list, name=" authors_list "),

]

views.py

from django.shortcuts import render

from books.models import Author

def authors_list (request, category ):

authors_list = Author.objects.filter(category=category)

context = {' authors_list' :

authors_list,

}

return render(request,

context,

'templates/

authors_list

.html')

Class based view

urls.py

from django.conf.urls import url

from .views import AuthorsList

urlpatterns = [

url(r'^authors-list/subject/(?P<category> )/$', AuthorsList.as_view(), name=" authors_list "),

]

views.py

from django.views.generic import ListView

from books.models import Author

class AuthorsList(ListView):

model = Author

template_name = "templates/ authors_list .html"

paginate_by = 10 # It will paginate the objects such that each page contains atmost 10 objects

def get_queryset(self):

query_set = self.model.objects.filter(category=self.kwargs.get('category

本文开发(python)相关术语:python基础教程 python多线程 web开发工程师 软件开发工程师 软件开发流程

主题: Django
分页:12
转载请注明
本文标题:Django - migrating from function based views to class based views
本站链接:http://www.codesec.net/view/532559.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(python) | 评论(0) | 阅读(47)