简体   繁体   English

Django“视图未返回HttpResponse对象。”

[英]Django “The view didn't return an HttpResponse object.”

I have a very basic view where I'm saving a form. 我有一个非常基本的视图,用于保存表单。 For some reason I keep getting a "view didn't return an HttpResponse object" error. 由于某种原因,我不断收到“视图未返回HttpResponse对象”错误。 I looked at this common issue here but haven't found a solution that applies to me. 我在这里查看了这个常见问题,但没有找到适用于我的解决方案。 does anyone have any thoughts? 有人有想法吗? I've included the code below. 我已包含以下代码。 Would greatly appreciate help with this simple question! 非常感谢您提供有关此简单问题的帮助!

def EarlyAdopterSignup(request):
    f = LandingPageForm(request.POST)
    if f.is_valid():
        email = f.cleaned_data['email']
        zip = f.cleaned_data['zip']
        adopter = EarlyAdopter(email = email, zip = zip)
        try:
            adopter.save()
            return render_to_response('EarlyAdopterSignup.html')
        except:
            return HttpResponse("There was an error with your submission. Please try again.")

To follow up on what AL is saying it is necessary to handle the case where the form IS NOT valid. 要遵循AL所说的话,有必要处理表格无效的情况。

This can be handled quite simply by passing a bound invalid form ( f ) back to your template. 通过将绑定的无效形式( f )返回给模板,可以非常简单地进行处理。

https://docs.djangoproject.com/en/dev/topics/forms/?from=olddocs#using-a-form-in-a-view https://docs.djangoproject.com/en/dev/topics/forms/?from=olddocs#using-a-form-in-a-view

Is an illustration of exactly what you need to do ^ 恰恰说明了您需要做什么^

def EarlyAdopterSignup(request):
    f = LandingPageForm(request.POST)
    if f.is_valid():
        email = f.cleaned_data['email']
        zip = f.cleaned_data['zip']
        adopter = EarlyAdopter(email = email, zip = zip)
        try:
            adopter.save()
            return render_to_response('EarlyAdopterSignup.html')
        except:
            return HttpResponse("There was an error with your submission. Please try again.")

   # handle where not valid
   return render(request, 'your_form_template.html', {
    'form': f,  # <- your invalid form instance
})

Well, to begin with, if your form isn't valid, you're not returning anything. 好吧,首先,如果您的表格无效,那么您将不会返回任何内容。 You should be reshowing the form with errors displayed. 您应该重新显示带有错误显示的表格。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 视图未返回HttpResponse对象。 它返回None instaed - View didn't return an HttpResponse object. It returned None instaed View 没有返回 HttpResponse 对象。 它返回 None 代替 - View didn't return an HttpResponse object. It returned None instead 该视图未返回 HttpResponse object。它返回 None - The view didn't return an HttpResponse object. It returned None instead python django ajax:ValueError:该视图未返回HttpResponse对象。 它返回None - python django ajax : ValueError: The view didn't return an HttpResponse object. It returned None instead Django:View没有返回HttpResponse对象。 它返回None - Django: View didn't return an HttpResponse object. It returned None instead 该视图未返回 HttpResponse 对象。 它使用 Django 返回 None - The view didn't return an HttpResponse object. It returned None instead, using Django django视图没有返回HttpResponse对象。 它返回None - django view didn't return an HttpResponse object. It returned None instead Django days_view没有返回HttpResponse对象。 它返回None - Django days_view didn't return an HttpResponse object. It returned None instead 值错误:视图未返回HttpResponse对象。 它返回None而不是Django - Value error: view didn't return an HttpResponse object. It returned None instead Django DJango-视图account.decorators.loginview没有返回HttpResponse对象。 它返回None - DJango - The view accounts.decorators.loginview didn't return an HttpResponse object. It returned None instead
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM