【问题标题】:The view customer_reg.views.Set_Checkout_Attributes didn't return an HttpResponse object视图 customer_reg.views.Set_Checkout_Attributes 未返回 HttpResponse 对象
【发布时间】:2012-09-18 05:48:39
【问题描述】:

我正在制作一个应用程序,在用户注册后,他会参考一个页面,在该页面上他可以单击一个链接来填写另一个订单详细信息。 当我单击该链接时,以下视图处于活动状态

def Checkout_Attributes(request):

    check_form = Check_Attribute_Form()
    context={'check_form':check_form}
    return render_to_response('checkout.html',context,context_instance=RequestContext(request))

提交表单后,以下视图处于活动状态

def Set_Checkout_Attributes(request):

    #if request.user.is_authenticated():
        #return HttpResponseRedirect('/checkout/')

    if request.method == 'POST':
        check_form = Check_Attribute_Form(request.POST)
            if check_form.is_valid():
            customer_check=Customer_check_attributes(billing_add=check_form.cleaned_data['billing_add'],shipping_add=check_form['shipping_add'],payment_method=check_form['payment_method'],shipping_method=check_form['shipping_method'],reward_points=check_form['reward_points'])
            customer_check.save()
            return render_to_response('sucess.html')
    else:
        check_form=Check_Attribute_Form()
        return render_to_response('a.html',{'check_form':check_form} , context_instance=RequestContext(request))  

在这个视图中,我收到 Set_Checkout_Attributes 没有返回 HttpResponse 对象的错误。

还有一件事,当我提交空白表单时,它没有显示验证错误

模板如下

<form action="/checkout1/" method="post">
{% csrf_token %}
{% if check_form.errors  %}<h2>Please correct the following fields:</h2>{% endif %}
<div class="register_div">
        {% if check_form.billing_add.errors %}<p class="error">{{ check_form.billing_add.errors }}</p>{% endif %}
    <p><label for="billing_add" {% if check_form.billing_add.errors %} class="error"{% endif %}>Billing Address:</label></p>
    <p>{{ check_form.billing_add }}</p>
</div>
.
.
.
.
.
<p><input type="submit" value="submit" aly="register"/></p>
</form>

【问题讨论】:

  • if check_form.is_valid(): 中的代码缩进是否正确。
  • 是的!!!!这里我使用的是多模型概念,Check_Attribute_Form 是第二个模型的形式

标签: django django-templates django-views


【解决方案1】:

首先,请为您的变量和视图使用正确的 PEP-8 名称。

其次,成功时不要渲染模板 - 重定向到新页面。

最后,想想你的观点的逻辑。表格无效时会发生什么?在那种情况下返回什么?再次查看文档中的示例,它准确显示了您应该使用的结构。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-06
    • 2014-07-27
    • 1970-01-01
    • 1970-01-01
    • 2022-11-25
    • 1970-01-01
    • 2020-11-01
    • 2014-12-03
    相关资源
    最近更新 更多