(13)Django框架學習-通用視圖


通用視圖

1. 前言

回想一下,在Django中view層起到的作用是相當於controller的角色,在view中實施的
動作,一般是取得請求參數,再從model中得到數據,再通過數據創建模板,返回相應
響應對象。但在一些比較通用的功能中,比如顯示對象列表,顯示某對象信息,如果反復
寫這么多流程的代碼,也是一件浪費時間的事,在這里,Django同樣給我們提供了類似的
"shortcut"捷徑--通用視圖。

2. 使用通用視圖

使用通用視圖的方法就是在urls.py這個路徑配置文件中進行,創建字典配置信息,然后
傳入patterns里的元組的第三個參數(extra-parameter),下面來看一個簡單的例子:
from django.conf.urls.defaults import *
from django.views.generic.simple import direct_to_template

urlpatterns = patterns('',
    url(r'^about/$', direct_to_template, {'template': 'about.html'}),
)
運行結果:
可以看到,沒有view的代碼,也可以直接運行。在這里direct_to_template,這個方法
,傳入第三個參數,然后直接進行渲染。
 
 
同時因為direct_to_template是一個函數,我們又可以把它放在view中,下面把
上面的例子改成匹配about/*,任意子網頁。
#urls.py
from django.conf.urls.defaults import *
from django.views.generic.simple import direct_to_template
from mysite.books.views import about_pages

urlpatterns = patterns('',
    (r'^about/$', direct_to_template, {
                                       'template': 'about.html'
                                      }),
    (r'^about/(\w+)/$', about_pages),
)
# view.py
from django.http import Http404
from django.template import TemplateDoesNotExist
from django.views.generic.simple import direct_to_template

#由正則匹配的參數
def about_pages(request, page):
    try:
        return direct_to_template(request, template="about/%s.html" % page)#返回的HttpResponse
    except TemplateDoesNotExist:
        raise Http404()
運行結果:
安全問題的題外話
上面的例子中,有一個潛在的安全問題,比較容易被忽略。那就是template="about/%s.html" % page這
句,這樣構造路徑容易被名為directory traversal的手段攻擊,簡單的說就是利用"../"這樣的返回父目錄的
路徑操作,去訪問原本不應該被訪問到的服務器上的文件,又被稱為dot dot slash攻擊。比如
使用"http://www.cnblogs.com/../etc/passwd"路徑的話,有可能就能讀取到服務器上的passwd這個文件,從而獲取到
關鍵密碼。
 
發布這篇博文的時候,cnblogs會把連續的"../"轉義成"http://www.cnblogs.com", 難道是在防止
dot dot slash攻擊?不信,你可以試試。
 
那在上面的例子中會不會有這個問題呢?
答案:不會的。。。
因為\w只會匹配數字,字母和下划線,不會去匹配dot這個符號。所以可以安心使用。
回車后,會直接退回到主頁,無法匹配。

3. 用於顯示對象內容的通用視圖

同樣,我們可以只需要model,urls.py文件就可以顯示對象的信息:
#model.py,之前例子中Publisher的定義
class Publisher(models.Model):
    name = models.CharField(max_length=30)
    address = models.CharField(max_length=50)
    city = models.CharField(max_length=60)
    state_province = models.CharField(max_length=30)
    country = models.CharField(max_length=50)
    website = models.URLField()

    def __unicode__(self):
        return self.name
#urls.py
from django.conf.urls.defaults import *
from django.views.generic import list_detail
from mysite.books.models import Publisher

publisher_info = {
    'queryset': Publisher.objects.all(),
    'template_name': 'publisher_list_page.html',
}

urlpatterns = patterns('',
    url(r'^publishers/$', list_detail.object_list, publisher_info),
)
#publisher_list_page.html
<h2>Publishers</h2>
    <ul>
        {% for publisher in object_list %}
        <li>{{ publisher.name }}</li>
        {% endfor %}
    </ul>
也是要構造一個字典參數,包含數據源和模板信息,再傳給list_detail.object_list方法,
然后直接完成渲染的工作。運行結果:

4. 通用視圖的幾種擴展用法

4.1 自定義結果集的模板名
上面的例子中 ,模板文件中的變量名一直是object_list,如果有多個數據需要顯示,那就
會,通用視圖提供了一種解決這種沖突的命名方法,就是在字典類型中加入template_object_name
變量,此變量+_list就組成模板文件中使用的變量名
publisher_info = {
    'queryset': Publisher.objects.all(),
    'template_name': 'publisher_list_page.html',
    'template_object_name': 'publisher',
}
模板文件也要作相應的修改:
<h2>Publishers</h2>
    <ul>
        {% for publisher in publisher_list %}
        <li>{{ publisher.name }}</li>
        {% endfor %}
    </ul>
運行結果同上。
4.2 增加額外的context
也是在字典變量中進行修改,增加"extra_context"變量,它的值就是額外的對象數據的字典描述,
就可以用在模板文件中使用字典描述中的key來當作變量名。
publisher_info = {
    'queryset': Publisher.objects.all(),
    'template_object_name': 'publisher',
    'template_name': 'publisher_list_page.html',
    'extra_context': {'book_list': Book.objects.all()}
}
模板文件也要做相應的改:
<h2>Publishers</h2>
    <ul>
        {% for publisher in publisher_list %}
        <li>{{ publisher.name }}</li>
        {% endfor %}
    </ul>
<h2>Book</h2>
    <ul>
        {% for book in book_list %}
        <li>{{ book.title }}</li>
        {% endfor %}
    </ul>
運行結果為:
上面的代碼又有一個問題,那就是'book_list': Book.objects.all(),這段代碼因為
在urls.py中,所以只會在第一次執行此路徑的時候執行一次,而不會因為Book的值
改變而改變,這是會使用到Django的緩存功能;而"queryset"中的值,Django是不會
緩存的,所以會隨着數據改變而改變。
 
解決方法就是使用函數引用來代替直接的返回值,任何在extra_context中的函數都會在
視圖每一次渲染的時候執行一次。所以代碼可以改成:
def get_books():
    return Book.objects.all()

publisher_info = {
    'queryset': Publisher.objects.all(),
    'template_object_name': 'publisher',
    'template_name': 'publisher_list_page.html',
    'extra_context': {'book_list': get_books},
}
或者改寫成:
publisher_info = {
    'queryset': Publisher.objects.all(),
    'template_object_name': 'publisher',
    'extra_context': {'book_list': Book.objects.all},
}
只要是引用參數就可以。
4.3 查看結果集的子集
方法很簡單,就是在字典數據中使用manage有的方法進行結果集操作,如filter等。
apress_books = {
    'queryset': Book.objects.filter(publisher__name='Apress'),
    'template_name': 'books/apress_list.html',
}
urlpatterns = patterns('',
    url(r'^books/apress/$', list_detail.object_list, apress_books),
)
4.4 更靈活的結果集操作
上面的代碼可以看到,需要把publisher的名字硬編碼在urls.py文件中,如何才能處理
從用戶傳遞過來的任何publisher名字呢?
答案就是把list_detail.object_list方法放在views.py中調用,就可以使用從request傳遞過來
的參數。因為list_detail.object_list也只不過是普通的python函數。
#urls.py
urlpatterns = patterns('',
    url(r'^publishers/$', list_detail.object_list, publisher_info),
    url(r'^books/(\w+)/$', books_by_publisher),
)
#views.py
from django.shortcuts import get_object_or_404
from django.views.generic import list_detail
from mysite.books.models import Book, Publisher

def books_by_publisher(request, name):

    # Look up the publisher (and raise a 404 if it can't be found).
    publisher = get_object_or_404(Publisher, name__iexact=name)

    # Use the object_list view for the heavy lifting.
    return list_detail.object_list(
        request,
        queryset = Book.objects.filter(publisher=publisher),
        template_name = 'books_by_publisher.html',
        template_object_name = 'book',
        extra_context = {'publisher': publisher}
)
list_detail.object_list返回的也是HttpResponse,
4.5 利用通用視圖做額外工作
利用4.4的功能,在執行完list_detail.object操作之后,不立即返回HttpResponse對象,而是
賦值給response變量,再進行一些額外的處理,最后再返回HttpResponse對象,這樣就可以
在使用通用視圖功能之前或者之后做一些處理操作。下面例子的功能是在每一次訪問作者之后
,都會更新作者的最后被訪問時間。
#urls.py
from mysite.books.views import author_detail

urlpatterns = patterns('',
    # ...
    url(r'^authors/(?P<author_id>\d+)/$', author_detail),
    # ...
)
#views.py
import datetime
from django.shortcuts import get_object_or_404
from django.views.generic import list_detail
from mysite.books.models import Author

def author_detail(request, author_id):
    # 執行通用視圖函數,但不立即返回HttpResponse對象
    response = list_detail.object_list(
        request,
        queryset = Author.objects.all(),
        object_id = author_id,
    )
    # 記錄訪問該作者的時間
    now = datetime.datetime.now()
    Author.objects.filter(id=author_id).update(last_accessed=now)
    # 返回通用視圖生成的HttpResponse對象
    return response
我們還可以修改HttpResponse對象的相關參數來達到改變響應信息的目的。比如
def author_list_plaintext(request):
    response = list_detail.object_list(
        request,
        queryset = Author.objects.all(),
        mimetype = 'text/plain',
        template_name = 'author_list.txt'
    )
    #修改響應格式,使其的內容不直接顯示在網頁中,而是儲存在文件中,下載下來。
    response["Content-Disposition"] = "attachment; filename=authors.txt"
    return response
模板文件author_list.txt的內容:
<h2>Author</h2>
<ul>
  {% for author in object_list %}
    <li>{{ author.first_name }}</li>
  {% endfor %}
</ul>
運行結果為生成authors.txt文件並提供下載:
文本內容會保留HTML標簽信息。






免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM