我正在尝试在模板上显示一个表单,但是我得到了一个很棒的错误:
渲染时捕获AttributeError:'WSGIRequest'对象没有属性'get'
错误在这一行:{%for form in form.visible_fields%}
我的看法 :
def view_discussion(request, discussion_id): discussion = get_object_or_404(Discussion, id=discussion_id) form = BaseMessageForm(request) return render(request,'ulule/discussions/view_discussion.html', { 'discussion':discussion, 'form':form, })
我的表格:
class BaseMessageForm(forms.Form): message_content = forms.CharField(widget=forms.HiddenInput())
我的模板:
非常感谢你的帮助 !
如果我没有记错,你所得到的错误是因为你有形式的初始化错误的签名:第一个参数是"数据",而你的情况存在于request.POST(而不是请求本身),如果你正在抵达POST.
通常,带有表单的视图看起来像这样:
def my_view(request, ...): if request.method == 'POST': # The form has been submitted form = MyForm(request.POST) if form.is_valid(): # do whatever you want here, save the form, etc else: form = MyForm() return render_to_response('myform.html', ... )