ton*_*njo 13 django formview view django-templates django-context
定义FormView派生类时:
class PrefsView(FormView):
template_name = "prefs.html"
form_class = MyForm # What's wrong with this?
def get(self,request):
context = self.get_context_data()
context['pagetitle'] = 'My special Title'
context['form'] = MyForm # Why Do I have to write this?
return render(self.request,self.template_name,context)
Run Code Online (Sandbox Code Playgroud)
我预计context['form'] = MyForm
不需要该行,因为form_class
已定义,但没有它{{ form }}
不会传递给模板.
我做错了什么?
Ala*_*air 14
在上下文中,form
应该是实例化的表单,而不是表单类.定义与form_class
在上下文数据中包含实例化表单完全分开.
对于你给出的例子,我认为你最好覆盖get_context_data
而不是get
.
def get_context_data(self, **kwargs):
context = super(PrefsView, self).get_context_data(**kwargs)
context['pagetitle'] = 'My special Title'
return context
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
10294 次 |
最近记录: |