Python Django表单错误未显示

Python Django表单错误未显示,python,django,Python,Django,我已经在stackoverflow和Internet上查找了这方面的信息,所以我将只显示我的代码 views.py 模板 {%csrf_令牌%} 卖场 {GIG表单%中f的%1} {{f.errors}} {{f.label_tag}}:{{f} {{f.help_text} {%endfor%} 这段代码似乎遵循正常的django表单协议,所以请告诉我为什么我的django模板没有显示错误。谢谢看起来您缺少了一个else块 如果gigform.valid()返回false,则覆盖变量“t

我已经在stackoverflow和Internet上查找了这方面的信息,所以我将只显示我的代码

views.py

模板


{%csrf_令牌%}
卖场
{GIG表单%中f的%1}
{{f.errors}}
{{f.label_tag}}:{{f}
{{f.help_text}
{%endfor%}


这段代码似乎遵循正常的django表单协议,所以请告诉我为什么我的django模板没有显示错误。谢谢

看起来您缺少了一个else块

如果gigform.valid()返回false,则覆盖变量“thegigform”。尝试按如下方式重新构造代码:

if request.method=='POST':
    #bind form with form inputs and image
    thegigform=GigForm(request.POST,request.FILES)
    if thegigform.is_valid():
        thegigform.title=gigform.cleaned_data['title']
        thegigform.description=gigform.cleaned_data['description']
        thegigform.more_info=gigform.cleaned_data['more_info']
        thegigform.time_for_completion=gigform.cleaned_data['time_for_completion']
        thegigform.gig_image=gigform.cleaned_data['gig_image']
        finalgigform=gigform.save(commit=False)
        finalgigform.from_user=theuser
        finalgigform.save()
        return HttpResponseRedirect('done')
else:
    thegigform=GigForm()
context=RequestContext(request)
return render_to_response('sell.html',{'theuser':theuser,'thegigform':thegigform},context_instance=context)

看起来您缺少了一个else块

如果gigform.valid()返回false,则覆盖变量“thegigform”。尝试按如下方式重新构造代码:

if request.method=='POST':
    #bind form with form inputs and image
    thegigform=GigForm(request.POST,request.FILES)
    if thegigform.is_valid():
        thegigform.title=gigform.cleaned_data['title']
        thegigform.description=gigform.cleaned_data['description']
        thegigform.more_info=gigform.cleaned_data['more_info']
        thegigform.time_for_completion=gigform.cleaned_data['time_for_completion']
        thegigform.gig_image=gigform.cleaned_data['gig_image']
        finalgigform=gigform.save(commit=False)
        finalgigform.from_user=theuser
        finalgigform.save()
        return HttpResponseRedirect('done')
else:
    thegigform=GigForm()
context=RequestContext(request)
return render_to_response('sell.html',{'theuser':theuser,'thegigform':thegigform},context_instance=context)

这就是我以前在代码中所做的,但有人告诉我要改变这一点,所以如果请求是get,我会将其他内容放回去捕获。问题没有解决你明白为什么在你发布的代码中你永远看不到错误吗?如果gigform.is_valid()返回False,那么您将使用与request.method相同的代码路径!='邮政局'。也就是说,您正在创建一个新的GigForm对象。要查看呈现的错误,您需要将上下文中的“thegigform”设置为导致gigform的同一对象。_valid()是否返回False。那么上下文将是gigform?我很抱歉我做这个有困难,你能告诉我代码应该是什么样子吗。thegigform=gigform…{'thegigform':thegigform}像那样吗?抱歉,我修复了问题,我将gigform重命名为“thegigform”,而不是像我在帖子中使用的那样使用gigform。谢谢你的帮助,虽然这是我以前代码中的内容,但有人告诉我要更改它,所以如果请求是get,我会将其他内容放回去捕获。问题没有解决你明白为什么在你发布的代码中你永远看不到错误吗?如果gigform.is_valid()返回False,那么您将使用与request.method相同的代码路径!='邮政局'。也就是说,您正在创建一个新的GigForm对象。要查看呈现的错误,您需要将上下文中的“thegigform”设置为导致gigform的同一对象。_valid()是否返回False。那么上下文将是gigform?我很抱歉我做这个有困难,你能告诉我代码应该是什么样子吗。thegigform=gigform…{'thegigform':thegigform}像那样吗?抱歉,我修复了问题,我将gigform重命名为“thegigform”,而不是像我在帖子中使用的那样使用gigform。谢谢你的帮助
if request.method=='POST':
    #bind form with form inputs and image
    thegigform=GigForm(request.POST,request.FILES)
    if thegigform.is_valid():
        thegigform.title=gigform.cleaned_data['title']
        thegigform.description=gigform.cleaned_data['description']
        thegigform.more_info=gigform.cleaned_data['more_info']
        thegigform.time_for_completion=gigform.cleaned_data['time_for_completion']
        thegigform.gig_image=gigform.cleaned_data['gig_image']
        finalgigform=gigform.save(commit=False)
        finalgigform.from_user=theuser
        finalgigform.save()
        return HttpResponseRedirect('done')
else:
    thegigform=GigForm()
context=RequestContext(request)
return render_to_response('sell.html',{'theuser':theuser,'thegigform':thegigform},context_instance=context)