from django.urls import reverse#Django 2.0 remove django.core.urlresolver
Django 2.0 移除了django.core.urlresolver 模塊 轉換爲django.urls 代替
python manage.py test --verbosity=2
Verbosity(冗長) determines the amount(數量) of notification(通知) and debug(調試) information that will be printed to the console(控制檯); 0 is no output(輸出), 1 is normal output, and 2 is verbose(冗長的) output
學習了第二章 superuser 管理員系統 Django有些強大 居然直接有後臺管理系統 雖然是全英文的文檔 但是簡潔的語言還是令人心曠神宜的
------------------------------------分割線------2/19----------------------------------------------
test 文件中 必須先在數據庫中創建新的實例
useful trick
Primary Key AutoField | |
---|---|
Regex(正則) | (?P<pk>\d+) |
Example | url(r'^questions/(?P<pk>\d+)/$', views.question, name='question') |
Valid URL | /questions/934/ |
Captures | {'pk': '934'} |
Slug Field | |
---|---|
Regex | (?P<slug>[-\w]+) |
Example | url(r'^posts/(?P<slug(鼻涕蟲)>[-\w]+)/$', views.post, name='post') |
Valid URL | /posts/hello-world/ |
Captures | {'slug': 'hello-world'} |
Slug Field with Primary Key | |
---|---|
Regex | (?P<slug>[-\w]+)-(?P<pk>\d+) |
Example | url(r'^blog/(?P<slug>[-\w]+)-(?P<pk>\d+)/$', views.blog_post, name='blog_post') |
Valid URL | /blog/hello-world-159/ |
Captures | {'slug': 'hello-world', 'pk': '159'} |
Django User Username | |
---|---|
Regex | (?P<username>[\w.@+-]+) |
Example | url(r'^profile/(?P<username>[\w.@+-]+)/$', views.user_profile, name='user_profile') |
Valid URL | /profile/vitorfs/ |
Captures | {'username': 'vitorfs'} |
Year | |
---|---|
Regex | (?P<year>[0-9]{4}) |
Example | url(r'^articles/(?P<year>[0-9]{4})/$', views.year_archive, name='year') |
Valid URL | /articles/2016/ |
Captures | {'year': '2016'} |
Year / Month | |
---|---|
Regex | (?P<year>[0-9]{4})/(?P<month>[0-9]{2}) |
Example | url(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/$', views.month_archive, name='month') |
Valid URL | /articles/2016/01/ |
Captures | {'year': '2016', 'month': '01'} |
{% load static %}
他必須放到最前面
一個註釋都不能有
------------------------------------分割線------2/20----------------------------------------------
base 創建模板
{% block breadcrumb %}
{% endblock %}
可以在裏面插入 breadcrumb 在他的後代頁面中
<title>{% block title %}Django Boards{% endblock %}</title>
當base頁面中設定好 內容之後所有的子後代都會繼承其中的內容
正則:$ 匹配爲末尾位置
from django.shortcuts import render
render(request, template_name, context=None, content_type=None, status=None, using=None)
render方法
此方法的作用---結合一個給定的模板和一個給定的上下文字典,並返回一個渲染後的 HttpResponse 對象。
通俗的講就是把context的內容, 加載進templates中定義的文件, 並通過瀏覽器渲染呈現.
resolve方法 :
from django.urls import resolve
作用:從URL得到url_name
reverse 是resolve的反向
assertContains(response, text, count=None, status_code=200, msg_prefix='', html=False)
斷言response是否與status_code和text內容相應。將html設爲True會將text作爲html處理。
<input type="text" class="form-control" id="id_subject" name="subject">this name
The name will be used to retrieve(檢索) the data on the server side.
在HTML 中使用board.topics.all:
Another important thing to note is that, inside Python code, we have to use parenthesis(插入語): board.topics.all()
, because all()
is a method. When writing code using the Django Template(模板) Language, in an HTML template file, we don’t use parenthesis, so it’s just board.topics.all
.
board.topics.filter(subject__contains='Hello')過濾
test文件中的很多方法和爬蟲有千絲萬縷的關係