加入收藏 | 设为首页 | 会员中心 | 我要投稿 百客网 - 域百科网 (https://www.yubaike.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

表单验证错误?

发布时间:2022-09-28 16:01:56 所属栏目:PHP教程 来源:
导读:  I'm trying to run and validate a form but having some problem. 我正在尝试运行和验证表单PHP表单验证,但遇到了一些问题。 Instead of displaying the form it displays the HttpResponse I put to disp
  I'm trying to run and validate a form but having some problem. 我正在尝试运行和验证表单PHP表单验证,但遇到了一些问题。 Instead of displaying the form it displays the HttpResponse I put to display when form is not valid. 代替显示表单,它显示我在表单无效时显示的HttpResponse。
 
  Here is my Model: 这是我的模特:
 
  class Preference(models.Model):
  CLASS_CHOICES = [('1', '1'), ('2', '2'), ('3', '3')]
  BOARD_CHOICES = [('C', 'CBSE'), ('I', 'ICSE'), ('S', 'State Board')]
  SLOT_CHOICES = [('M', 'Morning'), ('A', 'AfterNoon'), ('E', 'Evening')]
  SUBJECT_CHOICES = [('H', 'HINDI'), ('M', 'MATH'), ('E', 'ENGLISH')]
  LOCATION_CHOICES = [('M', 'My Home'), ('T', 'I am willing to travel')]
  GENDER_CHOICES = [('M', 'Male'), ('F', 'Female'), ('B', 'Both are Fine')]
  Class = models.CharField(max_length=2, choices=CLASS_CHOICES, default='1', blank=False)
  Board = models.CharField(max_length=2, choices=BOARD_CHOICES, default='C', blank=False)
  Subject = models.CharField(max_length=2, choices=SUBJECT_CHOICES, default='M', blank=False)
  Frequency = models.IntegerField(default=7)
  Slot = models.CharField(max_length=2, choices=SLOT_CHOICES, default='E', blank=False)
  Location = models.CharField(max_length=2, choices=LOCATION_CHOICES, default='M', blank=False)
  Gender = models.CharField(max_length=2, choices=GENDER_CHOICES, default='M', blank=False)
  Address = models.CharField(max_length=250, blank=True)
  Travel = models.IntegerField(default=5)
  Name = models.CharField(max_length=50, blank=True)
  Contact = models.IntegerField(default=100)
  Here is my form: 这是我的表格:
 
  class PreferenceForm(forms.ModelForm):
  class Meta:
      model = Preference
      fields = ['Class', 'Board', 'Subject', 'Frequency', 'Slot', 'Location', 'Gender', 'Address',
                'Travel', 'Name', 'Contact']
      widgets = {
             'Board': forms.RadioSelect(),
             'Subject': forms.CheckboxSelectMultiple(),
             'Slot': forms.CheckboxSelectMultiple(),
             'Location': forms.CheckboxSelectMultiple(),
             'Gender': forms.RadioSelect()  }
  And here is my view: 这是我的看法:
 
  def pref2(request):
  form = PreferenceForm(request.POST or None)
  if form.is_valid():
      prefer = form.save(commit=False)
      prefer.save()
      return HttpResponse('Order Complete')
  else:
      return HttpResponse('Some Problem')
  When I run the app, it just display the line 'Some Problem' without showing the form. 当我运行该应用程序时,它仅显示“ Some Problem”行,而不显示表单。 All help/suggestions are appreciated, thanks. 感谢所有的帮助/建议。
 
 

(编辑:百客网 - 域百科网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!