【发布时间】:2017-08-05 22:28:17
【问题描述】:
我正在编写一份使用无线电输入进行的调查。在 CMD 中,每次单击单选输入都会导致 request = POST,但是 form.is_valid 为 false,因为并非所有必需的按钮都被按下。我已经改变了它现在没有任何效果,它现在甚至不会请求。
下面是我的代码:
forms.py
class introForm(forms.ModelForm):
class Meta:
model = Intro
fields = ['name','education','sex',]
models.py
class Intro(models.Model):
EDUCATION_CHOICES = (
('1','1'),
('2','2'),
('3','3'),
('4','4'),
('G','Graduate'),
('P','Professor')
)
SEX_CHOICES = (
('M','Male'),
('F','Female'),
)
name = models.CharField(max_length = 10)
education = models.CharField(max_length = 1,choices = EDUCATION_CHOICES)
sex = models.CharField( max_length = 1, choices = SEX_CHOICES)
views.py
def get_User_Info(request):
#form_class = introForm(request.POST)
if request.method == 'POST':
if form_class.is_valid():
name = request.POST['name']
print(name)
sex = request.POST['sex']
education = request.POST['education']
intro.objects.create(
name = name,
sex = sex,
education = education
)
intro.save()
print (connection.queries)
return HttpResponseRedirect("/vote/") #Save data and redirect
else:
form = introForm()
return render(request, 'Intro.html', {'introForm': form})
Intro.html # 写得仓促,因为表单由于某种原因无法呈现自己...
<!-- Survey/templates/Intro.html -->
<!DOCTYPE html>
{% load staticfiles %}
<html>
<body>
<form action="" method="post" id = "introForm">
<p><label for="name">Name:</label>
<input id=name type="text" name="subject" maxlength="15" required /></p>
<p><label for="sex">Sex:</label>
<input type="radio" name="Sex" value="M" required>Male
<input type="radio" name="Sex" value="F" required>Female</p>
<p><label for="education">Education:</label>
<input type="radio" name="Education" value="1" required>1
<input type="radio" name="Education" value="2" required>2
<input type="radio" name="Education" value="3" required>3
<input type="radio" name="Education" value="4" required>4
<input type="radio" name="Education" value="G" required>Graduate
<input type="radio" name="Education" value="P" required>Professor</p>
</form>
<input type="submit" value="Next" id = "next"/>
</body>
</html>
提前致谢!
【问题讨论】: