2012-06-12 40 views
17

想要將我的項目更新到最新版本的Django,並發現通用視圖已經發生了相當大的變化。看看這些文檔,我發現他們將所有通用的東西改爲基於類的視圖。我理解大多數情況下的用法,但對於爲視圖返回大量對象時需要做什麼感到困惑。目前的網址可能如下所示:從direct_to_template移動到Django中的新TemplateView

(r'^$', direct_to_template, { 'template': 'index.html', 'extra_context': { 'form': CodeAddForm, 'topStores': get_topStores, 'newsStories': get_dealStories, 'latestCodes': get_latestCode, 'tags':get_topTags, 'bios':get_bios}}, 'index'), 

如何將這樣的內容轉換爲這些新視圖?

回答

29

Generic Views Migration描述了什麼基於類的視圖取代什麼。根據文檔,傳遞extra_context的唯一方法是TemplateView的子類並提供您自己的get_context_data方法。這是我想出的一個DirectTemplateView類,允許extra_contextdirect_to_template一樣。

from django.views.generic import TemplateView 

class DirectTemplateView(TemplateView): 
    extra_context = None 
    def get_context_data(self, **kwargs): 
     context = super(self.__class__, self).get_context_data(**kwargs) 
     if self.extra_context is not None: 
      for key, value in self.extra_context.items(): 
       if callable(value): 
        context[key] = value() 
       else: 
        context[key] = value 
     return context 

使用這個類,你將取代:

(r'^$', direct_to_template, { 'template': 'index.html', 'extra_context': { 
    'form': CodeAddForm, 
    'topStores': get_topStores, 
    'newsStories': get_dealStories, 
    'latestCodes': get_latestCode, 
    'tags':get_topTags, 
    'bios':get_bios 
}}, 'index'), 

有:

(r'^$', DirectTemplateView.as_view(template_name='index.html', extra_context={ 
    'form': CodeAddForm, 
    'topStores': get_topStores, 
    'newsStories': get_dealStories, 
    'latestCodes': get_latestCode, 
    'tags':get_topTags, 
    'bios':get_bios 
}), 'index'), 
+1

return direct_to_template(request, 'template.html', {'foo':12, 'bar':13}) 

本你的DirectTemplateView的語法是什麼? return direct_to_template(request,template ='template.html',extra_context) – mogga

+0

DirectTemplateView.as_view(template_name ='template.html',extra_context = extra_context) – Pykler

+0

我試過這個,但得到這個錯誤:'AttributeError at /管/ data_browse/ 'DirectTemplateView' 對象有沒有屬性 'has_header' 請求方法:\t GET 請求URL:\t的http://本地主機:8000 /管材/ data_browse/ Django的版本:\t 1.5.2 異常類型: \t屬性錯誤 異常值:\t 'DirectTemplateView'對象沒有屬性'has_header' 異常位置:\t /Library/Python/2.7/site-packages/django/utils/cache.py在patch_vary_headers,管線142 Python的可執行文件:\t的/ usr /斌/蟒 Python的版本:\t 2.7.2' – mobopro

4

我跑進與使用DirectTemplateView子Pykler的答案的一個問題。具體而言,這樣的錯誤:

AttributeError at /pipe/data_browse/ 'DirectTemplateView' object has no attribute 'has_header' Request Method: 
    GET Request URL: http://localhost:8000/pipe/data_browse/ Django Version: 1.5.2 
    Exception Type: AttributeError 
    Exception Value: 'DirectTemplateView' object has no attribute 'has_header' 
    Exception Location: /Library/Python/2.7/site-packages/django/utils/cache.py in patch_vary_headers, line 142 
    Python Executable: /usr/bin/python 
    Python Version: 2.7.2 

什麼工作對我來說是不是轉換任何像這樣的一行:如果我在一個視圖中使用使用direct_to_template

return render_to_response('template.html', {'foo':12, 'bar':13}, context_instance=RequestContext(request)) 
+0

Pykler的答案表明Pykler的答案將視圖對象返回給中間件,導致某些定製中間件出現問題,render_to_responce或render與direct_to_template類似地返回響應對象。目前這似乎是一種更向後兼容的方法。這也使得將Djano升級到1.5或1.6而不必將所有視圖轉換爲基於類的視圖變得非常簡單。 – arctelix

相關問題