gpt4 book ai didi

Django 1.2 - Pb 在模板中带有表单(WSGIRequest)

转载 作者:行者123 更新时间:2023-12-02 09:54:01 25 4
gpt4 key购买 nike

我试图在模板上显示表单,但出现了一个奇怪的错误:

Caught AttributeError while rendering: 'WSGIRequest' object has no attribute 'get'

错误在这一行:{% for field 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())

我的模板:

<form action="" method="post">
{% csrf_token %}
{% for field in form.visible_fields %}
<div class="fieldWrapper">
{% if forloop.first %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% endif %}

{{ field.errors }}
{{ field.label_tag }}: {{ field }}
</div>
{% endfor %}
<p><input type="submit" value="Send message" /></p>
</form>

非常感谢您的帮助!

最佳答案

如果我没记错的话,您遇到的错误是因为表单初始化程序的签名错误而发生的:它的第一个参数是“data”,在您的情况下,它驻留在 request.POST (而不是请求本身)中,如果您是通过 POST 到达的。

通常带有表单的 View 看起来像这样:

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', ... )

关于Django 1.2 - Pb 在模板中带有表单(WSGIRequest),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2453062/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com