Django表单始终显示错误“此字段为必填", [英] Django form always shows error "This field is required"

查看:67
本文介绍了Django表单始终显示错误“此字段为必填",的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我渲染表单时,django总是在每个字段上显示一条错误消息此字段是必需的" 即使未提交表单.可能是什么问题?

When I am rendering my form django always shows an error message on each field "This field is required" even though the form is not submitted. What could be the problem?

这是我的模特表格

class MMEditidStateForm(forms.ModelForm):
  class Meta:
     model = models.MMEditidState
     exclude = ('status_id',)

这是我的模特

class MMEditidState(models.Model):
  state_id = models.IntegerField(primary_key = True)
  state_dremelid = models.ForeignKey(MMDremelDump, db_column = 'state_dremelid')
  assignee = models.CharField(max_length = 50)
  state = models.CharField(max_length = 50)
  role = models.CharField(max_length = 50)
  date = models.DateTimeField() 
  class Meta:
     db_table = u'mm_editid_state'
  def __unicode__(self):
     return u'%s %s' % (self.state_dremelid, self.assignee)

这是我的观点

def qcthisedit(request, get_id):
  dremel_id = MMEditidState.objects.filter(pk=get_id).values('state_dremelid')
  if request.method == "POST":
     form = forms.MMEditidStateForm(request.POST)
     if form.is_valid():
        form.save()
      return http.HttpResponseRedirect('/mmqc/dremel_list/')
  else:
     form = forms.MMEditidStateForm(request.POST)
  return shortcuts.render_to_response('qcthisedit.html',locals(),
                                  context_instance = context.RequestContext(request))

现在我只是将模板中的表单呈现为

Now I am just rendering my form in the template as

    <table>
    <h3>Submit this form if there are no errors</h3>

    <form action="." method="post">
    {{form.as_table}}      
    </table>
    <input type="submit" value="Submit">
    <INPUT TYPE="BUTTON" VALUE="Go Back" ONCLICK="history.go(-1)"></form><br> 

请让我知道可能是什么问题?以前我没有收到此错误消息

Please let me know what could be the problem? Previously I didn't got this error message

推荐答案

因为您总是使用request.POST实例化表单,即使您实际上并未在表单上发布表单.在您的else子句中,放置request.POST.

Because you're always instantiating your form with request.POST, even when you're not actually posting to it. In your else clause, drop the request.POST.

这篇关于Django表单始终显示错误“此字段为必填",的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆