2013-11-24 29 views
0

我正在遷移我們的舊應用程序到Django 1.6。django遷移到1.6的舊應用程序:ListView

現在,一些觀點已經被編程這樣:

from django.views.generic.list_detail import object_list 

@render_to("items/index.html") 
def index(request): 
    profile = request.user.get_profile()  
    args = clean_url_encode(request.GET.copy().urlencode()) 
    context = { 
     'is_dashboard': True, 
     'body_id': 'dashboard', 
     'object_list': None, 
     'args':args, 
     'show_in_process':False 
    } 
    return context 

我知道,我現在需要使用新的ListView,但實例和文檔似乎不告訴我,我有這種特殊情況下:在上下文中傳遞object_list。

我該如何調整此代碼以使用基於類的新通用視圖?我是否也可以只使用ListView.asView()而不是'object_list':None ?

回答

0

如果您沒有對象列表,爲什麼使用ListView?我認爲TemplateView應該完成這項工作。你只需要重寫get_context_data,並提供自定義上下文

class IndexView(TemplateView): 
    template_name = 'items/index.html' 

    def get_context_data(self, **kwargs): 
     context = super(IndexView, self).get_context_data(**kwargs) 
     profile = self.request.user.get_profile() 
     args = clean_url_encode(self.request.GET.copy().urlencode()) 
     context.update({ 
      'is_dashboard': True, 
      'body_id': 'dashboard', 
      'object_list': None, 
      'args':args, 
      'show_in_process':False 
     }) 
     return context 
+0

感謝@tuxcanfly,所以如果我沒有理解好,每一個這樣的功能變成了自己的視圖類?這將是相當多的遷移我們... – faboolous

+0

我明白了。不是所有的功能,只是通用的看法。 – faboolous

相關問題