2013-06-24 45 views
2

我有一個類視圖,它使用FormView。我需要改變形式,即名稱這是它曾經是我的老功能的看法:Django如何使用FormView重命名上下文對象?

upload_form = ContactUploadForm(request.user) 
context = {'upload': upload_form,} 

與我的新觀點,我假設我可以用get_context_data方法,但不知道該如何重新命名。

How can I rename this form to **upload** not **form** as my templates uses `{{ upload }}` not `{{ form }}`? Thanks. 

當前類視圖:

class ImportFromFile(FormView): 

    template_name = 'contacts/import_file.html' 
    form_class = ContactUploadForm 

    def get_context_data(self, **kwargs): 
     """ 
     Get the context for this view. 
     """ 
     # Call the base implementation first to get a context. 
     context = super(ImportFromFile, self).get_context_data(**kwargs) 

     return context 

回答

9

試試這個:

class ImportFromFile(FormView): 

    template_name = 'contacts/import_file.html' 
    form_class = ContactUploadForm 

    def get_context_data(self, **kwargs): 
     """ 
     Get the context for this view. 
     """ 
     kwargs['upload'] = kwargs['form'] 
     del kwargs['form'] 
     return super(ImportFromFile, self).get_context_data(**kwargs) 
+0

不錯,謝謝! – GrantU