Digango Views

  • Understanding Django Views
    • avoid add business logic into the view, if embedding business logic directly into the view will make it nonreusable, not testability and coupling.
  • Django’s Generic Views
    • from django.views.generic import ListView, DetailView, TemplateView
    • from django.views.generic.dates import ArchiveIndexView, YearArchiveView, MonthArchiveView, DayArchiveView, TodayArchiveView, DateDetailView
    • from django.views.generic.edit import FormView, CreateView, DeleteView, UpdateView, DeleteView
    • use help(ListView) to check detail property and method and so on
  • Mixins
    • refers to a class that provides additional functionality to other classes through inheritance.
  • Django Mixin
    • ContextMixin: Adds extra context data to the view
    • TemplateResponseMixin: Renders template and returns an HTTP response
    • FormMixin: Used to handle form submission and validation
    • CreateModelMixin: Used to save a new object to database
    • UpdateModelMixin: Used to upate an existing object in database
    • DeleteModelMixin: Used to delete an object
  • URL Configuration
    • ulrpatterns = [ re_path(r’^tasks/(?P<by_date>[0-9]{4}-[0-9]{2}-[0-9]{2})/$’, views.query_by_date, name=”task_list”),]

^: indicating the begin

$: indicateing the last

[0-9]{4}: 4 characters and each character is 0~9.

(?P<by_date>…….), will pass the value as by_date parameter to the view.

r preceding the string is a flag, informing Python that this is a raw string

  • url path built-in convert:
    • str
    • int
    • slug
    • uuid
    • path
  • url path custom convert
  • URL Namespace and Name tasks:task-detail

to use namespace for urls, use app_name in the urls.py

  • HttpRequest object
    • request.method
    • request.user
    • request.path
    • request.GET
    • request.POST
    • request.FIELS
    • request.headers
  • HttpResponse object
    • the object accept a parameter that contains the content destined for client.
分類: Django,標籤: 。這篇內容的永久連結

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *