我有一个使用 FormView 的类 View 。我需要更改表单的名称,即在我的旧函数 View 中以前是这样的:
upload_form = ContactUploadForm(request.user)
context = {'upload': upload_form,}
在我的新 View 中,我假设我可以使用 get_context_data 方法重命名,但不确定如何重命名。我如何将此表单重命名为 上传 而不是 表单 因为我的模板使用
{{ upload }}
而不是 {{ form }}
?谢谢。当前类 View :
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
最佳答案
试试这个:
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.pop('form')
return super(ImportFromFile, self).get_context_data(**kwargs)
关于Django 如何通过 FormView 重命名上下文对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17272614/