-
1、FBV和CBV
-
2、request對象和response對象
-
3、路由系統
1、FBV和CBV
1.FBV(function base view)基於函數的視圖
2.CBC(class base view)基於類的視圖
3.二者區別以下:
#FBV
def add_author(request):
if request.method == 'POST':
new_name = request.POST.get('add_name')
new_books = request.POST.getlist('books')
ret = models.Author.objects.create(name=new_name)
ret.book.set(new_books)
ret.save()
return redirect('/author_list/')
ret = models.Book.objects.all()
return render(request, 'add_author.html', {'book_list': ret})
#CBV
#導入
from django.views import View
#必須繼承View
class AddAuthor(View):
#若是是get方法
def get(self , request):
pass
#若是是post方法
def post(self, request):
pass
url(r'^add_author/', views.AddAuthor.as_view()),
#必需要這樣調用類的方法
4.結論:
二者各有優劣,類的邏輯更加清晰一點
2、request對象和reponse對象
1.request相關的經常使用值:
path_info 返回用戶訪問url,不包括域名和端口
method 請求中使用的HTTP方法的字符串表示,全大寫表示。
GET 包含全部HTTP GET參數的類字典對象
POST 包含全部HTTP POST參數的類字典對象
body 請求體,byte類型 request.POST的數據就是從body裏面提取到的
2.上傳文件的簡單實例:
<form action="/test/" method="
post"
enctype="multipart/form-data">
#後面這個必定要寫
<input type="
file" name="file">
<input type="submit" name="上傳">
</form>
def test(request):
if request.method == 'POST':
#從請求的FILES中獲取上傳文件的文件名,file爲頁面上type=files類型的input的name屬性值
filename = request.FILES['file'].name
#在項目目錄下新建一個文件
with open(filename, 'wb') as f:
#從上傳的文件對象中一點一點讀
for chunk in request.FILES['file'].chunks():
f.write(chunk)
return HttpResponse('上傳成功')
而後在網頁上上傳文件就能夠接收了
3.response相關:
a.HttpResponse
-->返回字符串
b.
render
-->返回一個html頁面
c.redirect -->返回一個重定向(告訴瀏覽器訪問另一個網站)
d.JsonResponse
最原始的寫法:
加json.dumps():
def base(request):
data = {'name':'湯姆', 'age':18}
import json
return HttpResponse(
json.dumps(data))
#把data序列化稱json格式的字符串
#頁面返回的結果
{"name": "\u6c64\u59c6", "age": 18}
不加json.dumps():
def base(request):
data = {'name':'湯姆', 'age':18}
import json
return HttpResponse(data)
django封裝後的寫法:
def base(request):
data = {'name':'湯姆', 'age':18}
from django.http import JsonResponse
return JsonResponse(data)
#頁面返回的結果
{"name": "\u6c64\u59c6", "age": 18}
可是,django封裝的會出現一個問題:只能接收字典,不能接收列表,須要把安全監測關閉,以下:
from django.http import JsonResponse
return JsonResponse(data,
safe=False
)
3、路由系統
1.基本格式:
from django.conf.urls import url
urlpatterns = [
url(正則表達式, views視圖函數,參數,別名),
]
django2.0版本中的路由系統已經替換成下面的寫法:
from django.urls import path
urlpatterns = [
path('articles/2003/', views.special_case_2003),
path('articles/<int:year>/', views.year_archive),
path('articles/<int:year>/<int:month>/', views.month_archive),
path('articles/<int:year>/<int:month>/<slug:slug>/', views.article_detail),
]
2.參數說明:
正則表達式:一個正則表達式字符串
views視圖函數:一個可調用對象,一般爲一個視圖函數或一個指定視圖函數路徑的字符串
參數:可選的要傳遞給視圖函數的默認參數(字典形式)
別名:一個可選的name參數
3.基本配置:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^articles/2003/$', views.special_case_2003),
url(r'^articles/([0-9]{4})/$', views.year_archive),
url(r'^articles/([0-9]{4})/([0-9]{2})/$', views.month_archive),
url(r'^articles/([0-9]{4})/([0-9]{2})/([0-9]+)/$', views.article_detail),
]
4.注意事項:
a. urlpatterns中的元素按照書寫順序從上往下逐一匹配正則表達式,
一旦匹配成功則再也不繼續。
b. 若要從URL中捕獲一個值,只須要在它周圍放置一對
圓括號(分組匹配)。
c.
不須要添加一個前導的反斜槓,由於每一個URL 都有。例如,應該是^articles 而不是 ^/articles。
d. 每一個正則表達式前面的'r' 是可選的可是
建議加上。
5.url自動加/的問題:
# 是否開啓URL訪問地址後面不爲/跳轉至帶有/的路徑的配置項
# 把這行代碼直接加載settings最後一行就ok了
APPEND_SLASH=True
Django settings.py配置文件中默認沒有 APPEND_SLASH 這個參數,但 Django 默認這個參數爲 APPEND_SLASH = True。 其做用就是自動在網址結尾加'/'。
其效果就是:
咱們定義了urls.py:
from django.conf.urls import url
from app01 import views
urlpatterns = [
url(r'^blog/$', views.blog),
]
訪問 http://www.example.com/blog 時,默認將網址自動轉換爲 http://www.example/com/blog/ 。
若是在settings.py中設置了 APPEND_SLASH=False,此時咱們再請求 http://www.example.com/blog 時就會提示找不到頁面。
6.分組命名匹配:
上面的示例使用簡單的正則表達式分組匹配(經過圓括號)來
捕獲URL中的值並以位置參數形式傳遞給視圖。
在更高級的用法中,能夠使用
分組命名匹配的正則表達式組來捕獲URL中的值並以關鍵字參數形式傳遞給視圖。
在Python的正則表達式中,分組命名正則表達式組的語法是
(?P<name>pattern),其中name是組的名稱,pattern是要匹配的模式。
下面是以上URLconf 使用命名組的重寫:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^articles/
2003/$', views.special_case_2003),
url(r'^articles/(?P<year>[0-9]{4})/$', views.year_archive),
url(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/$', views.month_archive),
url(r'^articles/
(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/(?P<day>[0-9]{2})/$', views.article_detail),
]
這個實現與前面的示例徹底相同,只有一個細微的差異:捕獲的值做爲
關鍵字參數而不是
位置參數傳遞給視圖函數。
例如,針對url /articles/2017/12/至關於按如下方式調用視圖函數:
views.month_archive(
request, year="2017", month="12")
在實際應用中,
使用分組命名匹配的方式可讓你的URLconf 更加明晰且不容易產生參數順序問題的錯誤,可是有些開發人員則認爲分組命名組語法太醜陋、繁瑣。
至於究竟應該使用哪種,你能夠根據本身的喜愛來決定。
7.視圖函數中的默認指定值:
# urls.py中
from django.conf.urls import url
from . import views
urlpatterns = [
# 默認返回第一頁的blog
url
(
r'^blog/$', views.page),
# 返回你訪問的具體頁面的blog
url(
r'^blog/page(?P<num>[0-9]+)/$', views.page),
]
# views.py中,能夠爲num指定默認值
def page(request,
num="1"):
pass
在上面的例子中,兩個URL模式指向相同的view - views.page - 可是第一個模式並無從URL中捕獲任何東西。
若是第一個模式匹配上了,page()函數將使用其默認參數num=「1」,若是第二個模式匹配,page()將使用正則表達式捕獲到的num值。
8.反向解析URL:
本質上就是給url匹配模式起別名,而後用過別名拿到具體的URL路徑
a. 怎麼起別名?
在url匹配模式中,定義name="別名"
url(r'^test/', views.test, name='test')
2. 如何使用?
1. 在模板語言裏面使用:
{% url "別名" %} --> 獲得具體的URL路徑
2. 在視圖中如何使用:
from django.urls import reverse
reverse("別名")
--> 獲得具體的URL路徑
3. 如何傳參數?
1. 模板語言中:
{% url "別名" 2018 "nb" %}
2. 視圖函數中
傳位置參數:reverse("別名",
args=(2018, "nb"))
傳關鍵字參數:reverse("別名"
kwargs={"year": 2018, "title": "nb"})
4. namespace
爲了防止不一樣的app下面的url匹配模式有重複的別名