Study Notes of Django
- The model layer
- The view layer
- The template layer
[toc]
Project Structure
docs
: documentation- scripts
manage.py
: installed to PATH via setup.py
- [project_name]
- apps: project-specific applications
- settings: settings for different environments, see below
- urls.py
- wsgi.py
- static: site-specific static files
- templates: site-specific templates
- tests: site-specific tests (mostly in-browser ones)
- tmp: excluded from git
- setup.py
The model layer
A model is the single, definitive source of information about your data. It contains the essential fields and behaviors of the data you’re storing. Generally, each model maps to a single database table.
- Each model is a Python class that subclasses
django.db.models.Model
. - Each attribute of the model represents a database field.
Defined the model
1 | from django.db import models |
Using models
- Once you have defined your models, you need to tell Django you’re going to use those models.
- Do this by editing your settings file and changing the
INSTALLED_APPS
setting to add the name of the module that contains yourmodels.py
.
1 | INSTALLED_APPS = [ |
Model methods
- Define custom methods on a model to add custom “row-level” functionality to your objects.
- Whereas
Manager
methods are intended to do “table-wide” things, model methods should act on a particular model instance.
1 | from django.db import models |
- basic operations
1 | # 增:增加一条数据,可以接受字典类型数据 **kwargs |
- Double Underscore
1 | # Get the number |
Making queries
Retrieving specific objects with filters
The
QuerySet
returned byall()
describes all objects in the database table. Usually, though, you’ll need to select only a subset of the complete set of objects.To create such a subset, you refine the initial
QuerySet
, adding filter conditions. The two most common ways to refine aQuerySet
are:Field lookups
Basic lookups keyword arguments take the form
field__lookuptype=value
. (That’s a double-underscore).1
2
3
4# Django
Entry.objects.filter(pub_date__lte='2006-01-01')
# SQL
SELECT * FROM blog_entry WHERE pub_date <= '2006-01-01';Filters can reference fields on the model
1
2
3
4
5
6
7
8
9# To find all the blog entries with more than twice as many comments as pingbacks, we modify the query:
Entry.objects.filter(number_of_comments__gt=F('number_of_pingbacks') * 2)
# To find all the entries where the rating of the entry is less than the sum of the pingback count and comment count:
Entry.objects.filter(rating__lt=F('number_of_comments') + F('number_of_pingbacks'))
# An F() object with a double underscore will introduce any joins needed to access the related object.
# To retrieve all the entries where the author’s name is the same as the blog name:
Entry.objects.filter(authors__name=F('blog__name'))
The view layer
Writing views
A view function, or view for short, is a Python function that takes a Web request and returns a Web response.
For the sake of putting the code somewhere, the convention is to put views in a file called
views.py
, placed in your project or application directory.Simple example
The view returns an
HttpResponse
object that contains the generated response.Each view function is responsible for returning an
HttpResponse
object.- To display this view at a particular URL, you’ll need to create a URLconf; see URL dispatcher for instructions.
1
2
3
4
5
6
7
8
9
10
11
12
13# import the class HttpResponse from the django.http module, along with Python’s datetime library.
from django.http import HttpResponse
import datetime
def current_datetime(request):
"""
This is the view function.
We define a function called current_datetime.
Each view function takes an HttpRequest object as its first parameter, which is typically named request.
"""
now = datetime.datetime.now()
html = "<html><body>It is now %s.</body></html>" % now
return HttpResponse(html)
URL dispatcher
To design URLs for an app, you create a Python module informally called a URLconf (URL configuration). This module is pure Python code and is a mapping between URL path expressions to Python functions (your views).
How Django processes a request
- Django determines the root URLconf module to use. Ordinarily, this is the value of the
ROOT_URLCONF
setting, but if the incomingHttpRequest
object has aurlconf
attribute (set by middleware), its value will be used in place of theROOT_URLCONF
setting. - Django loads that Python module and looks for the variable
urlpatterns
. This should be a sequence ofdjango.urls.path()
and/ordjango.urls.re_path()
instances.- re_path(): The
route
argument should be a string orgettext_lazy()
(see Translating URL patterns) that contains a regular expression compatible with Python’sre
module.
- re_path(): The
- Django determines the root URLconf module to use. Ordinarily, this is the value of the
Simple Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22from django.urls import path
from . import views
# uising path
urlpatterns = [
path('articles/2003/', views.special_case_2003), # To capture a value from the URL, use angle brackets.
path('articles/<int:year>/', views.year_archive), # Captured values can optionally include a converter type.
path('articles/<int:year>/<int:month>/', views.month_archive),
path('articles/<int:year>/<int:month>/<slug:slug>/', views.article_detail),
]
# /articles/2003/03/building-a-django-site/ would match the final pattern.
# Django would call the function views.article_detail(request, year=2003, month=3, slug="building-a-django-site").
# using re_path
urlpatterns = [
path('articles/2003/', views.special_case_2003),
re_path(r'^articles/(?P<year>[0-9]{4})/$', views.year_archive),
re_path(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/$', views.month_archive),
re_path(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/(?P<slug>[\w-]+)/$', views.article_detail),
]Path converters
str
- Matches any non-empty string, excluding the path separator,'/'
. This is the default if a converter isn’t included in the expression.int
- Matches zero or any positive integer. Returns anint
.slug
- Matches any slug string consisting of ASCII letters or numbers, plus the hyphen and underscore characters. For example,building-your-1st-django-site
.uuid
- Matches a formatted UUID. To prevent multiple URLs from mapping to the same page, dashes must be included and letters must be lowercase. For example,075194d3-6885-417e-a8a8-6c931e272f00
. Returns aUUID
instance.path
- Matches any non-empty string, including the path separator,'/'
. This allows you to match against a complete URL path rather than a segment of a URL path as withstr
.
The template layer
Templates
Variables
- A variable outputs a value from the context, which is a dict-like object mapping keys to values
- Dictionary lookup, attribute lookup and list-index lookups are implemented with a dot notation
1
2
3
4
5My first name is {{ first_name }}. My last name is {{ last_name }}.
{{ my_dict.key }}
{{ my_object.attribute }}
{{ my_list.0 }}Tags
- Tags provide arbitrary logic in the rendering process.
- This definition is deliberately vague. For example, a tag can output content, serve as a control structure e.g. an “if” statement or a “for” loop, grab content from a database, or even enable access to other template tags.
1
2
3{% csrf_token %}
{% cycle 'odd' 'even' %}
{% if user.is_authenticated %}Hello, {{ user.username }}.{% endif %}Filters
- Filters transform the values of variables and tag arguments.
1
2{{ django|title }}
{{ my_date|date:"Y-m-d" }}Comments
1
{# this won't be rendered #}
Python programmers
Overview
Loading a template
- The recommended way to create a
Template
is by calling the factory methods of theEngine
:get_template()
,select_template()
andfrom_string()
. - In a Django project where the
TEMPLATES
setting defines aDjangoTemplates
engine, it’s possible to instantiate aTemplate
directly. If more than oneDjangoTemplates
engine is defined, the first one will be used.
1
2
3from django.template import Template
template = Template("My name is {{ my_name }}.")- The recommended way to create a
Rendering a context
1
2
3
4
5
6
7
8
9
10from django.template import Context, Template
"My name is {{ my_name }}.") template = Template(
"my_name": "Adrian"}) context = Context({
template.render(context)
"My name is Adrian."
"my_name": "Dolores"}) context = Context({
template.render(context)
"My name is Dolores."
SOME TIPS
VIEWS
1 | # url: mypage.com/?page=2 |
Study Notes of Django
http://vincentgaohj.github.io/Blog/2020/09/29/Study-Notes-of-Django/