天天看點

learn Django 筆記 遇到的坑!

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>[\[email protected]+-]+)

Example

url(r'^profile/(?P<username>[\[email protected]+-]+)/$', 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檔案中的很多方法和爬蟲有千絲萬縷的關系