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

时间:2010-03-16 08:58:26

标签: django

我正在尝试在模板上显示表单,但是我收到了一个很棒的错误:

  

渲染时捕获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())

我的模板:

<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>

非常感谢你的帮助!

1 个答案:

答案 0 :(得分:6)

如果我没记错的话,你得到的错误是因为你得到了表单初始化程序的签名错误:它的第一个参数是“data”,在你的情况下它位于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', ... )