Django - 뷰 레이어 - 클래스 뷰

5208 단어 Django 진급django
카탈로그
  • 일반적인 방법일 때 Django가 제공하는 각종 보기 종류를 계승한다
  • URLconf에서 as 필요view () 이 클래스 방법은 클래스 기반 보기를 함수 형식의 인터페이스로 변환합니다
  • 책 목록 보기를 얻는 예
  • Djanog은 다양한 응용 장면에 적용되는 기본적인 보기 종류를 많이 제공하는데 이런 종류의 보기는 모두 계승된다django.views.generic.base.View류.예컨대RedirectView 리디렉션에 사용,TemplateView 렌더링 템플릿에 사용됩니다.
    일반적인 방법일 때 Django가 제공하는 다양한 보기 클래스를 계승합니다
    from django.views.generic import TemplateView
    
    class AboutView(TemplateView):
    	template_name = 'about.html'
    

    URLconf에서 as 필요view () 이 클래스 방법은 클래스 기반 보기를 함수 형식의 인터페이스로 변환합니다
    from django.urls import path
    from some_app.views import AboutView
    
    urlpatterns = [
    	path('about/', AboutView.as_view()),
    ]
    

    책 목록 보기 보기 보기 가져오기
    경로
    from django.urls import path
    from books.views import BookListView
    
    urlpatterns = [
    	path('books/', BookListView.as_view()),
    ]
    

    from django.http import HttpResponse
    from django.views.generic import ListView
    from books.models import Book
    
    class BookListView(ListView):
    	model = Book  #     
    	
    	def head(self, request, *args, **kwargs):
    		last_book = self.get_queryset().latest('publication_date')
    		response = HttpResponse()
    		response['Last_Modified'] = last_book.publication_date.strftime(%a, %d %b %Y %H:%M:%S GMT)
    		return response
    

    좋은 웹페이지 즐겨찾기