【问题标题】:Form not generating/form is_valid = 0 for radio input forms表单未生成/表单 is_valid = 0 用于无线电输入表单
【发布时间】: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>  

提前致谢!

【问题讨论】:

    标签: python html django forms


    【解决方案1】:
    form_class = introForm(request.POST)
    

    您已将此变量命名为 form_class 而不是 form。您应该将其命名为与 else 语句中的表单相同,以便您可以实现所需的功能。此外,它应该是 if 语句中的第一行,如下所示:

    if request.method == 'POST':
        form = introForm(request.POST)
        if form.is_valid():
            ....
    

    你是不是这样在模板中使用form标签...

    {{ introForm }}
    

    【讨论】:

    • 您的表单是否确定发布到该视图?检查表单操作属性并确保其指向该视图的 url
    • 是的……我只是仔细检查了一遍。 CMD 只显示 get /thewebpage/ 然后无论我点击什么都不会更新...编辑:我添加了 forms.py
    猜你喜欢
    • 2016-11-19
    • 2010-12-19
    • 1970-01-01
    • 1970-01-01
    • 2016-08-01
    • 1970-01-01
    • 2013-06-17
    • 2017-05-27
    • 2022-01-02
    相关资源
    最近更新 更多