Django is_valid()返回false

时间:2017-08-14 12:11:32

标签: python django

  
    

我是Django的新手。每当我提交我的注册表单时,form.is_valid()在我的视图函数中返回false。我无法弄清楚我哪里出错了。我使用SignUpForm类来获取其他字段。 signup.html是我的模板。注册是我的视图功能。我的forms.py中的SignUpForm类是错误的吗?任何帮助都会很棒。我还在我的注册表单中填写了所有细节,但仍然是form.is_valid()在我的情况下返回false。

  

我的观看功能

def signup(request):
    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if form.is_valid():
            form.save()
            return render(request, 'signup.html', {'message': 'Signed Up successfully'})
    return render(request, 'signup.html')

my forms.py

from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User


class SignUpForm(UserCreationForm):
    first_name = forms.CharField(max_length=255)
    last_name = forms.CharField(max_length=255)
    password = forms.CharField(widget=forms.PasswordInput)
    confirm_password = forms.CharField(widget=forms.PasswordInput)
    phone_number = forms.CharField(max_length=100)

    class Meta:
        model = User
        fields = ['username', 'email', 'first_name', 'last_name', 'password', 'confirm_password', 'phone_number']

我的base.html

<body style = "margin: 0;">
    <div class="row" style="padding-left: 2%;padding-top:1%;">
        <div class="col-md-9">
            <h2>Welcome to my site</h2>
        </div>
        <div class="col-md-2">
            <a href="{% url 'login' %}" class="btn btn-primary" style="margin-right: 6%;">Login</a><br><br><br>
        </div>
    </div>


    <div class="row" style="margin-left: 4%">
        <form  class="form-group" name="form1" id="form1" method="post" style="padding-top: 1%" action = "{% url 'signup' %}">
            {% csrf_token %}
            <h3>Sign up</h3><br>
            <div class="form-group row">
                <label id="user" class="col-4 col-form-label">Username</label>
                <div class="col-8">
                    <input class="form-control" type="text" name = "username" placeholder = "Username" required="true" >
                </div>
            </div>
            <div class="form-group row">
                <label id="email" class="col-4 col-form-label">Email id</label>
                <div class="col-8">
                    <input class="form-control" type="text" name = "email" placeholder = "Email id" required="true" >
                </div>
            </div>
            <div class="form-group row">
                <label id="first_name" class="col-4 col-form-label">First name</label>
                <div class="col-8">
                    <input class="form-control" type="text" name = "first_name" placeholder = "First name" required="true" >
                </div>
            </div>
            <div class="form-group row">
                <label id="last_name" class="col-4 col-form-label">Last name</label>
                <div class="col-8">
                    <input class="form-control" type="text" name = "last_name" placeholder = "Last name" required="true" >
                </div>
            </div>
            <div class="form-group row">
                <label id="password" class="col-4 col-form-label">Password</label>
                <div class="col-8">
                    <input class="form-control" type="password" name = "password" placeholder = "Password" required="true" >
                </div>
            </div>
            <div class="form-group row">
                <label id="confirm_password" class="col-4 col-form-label">Re-enter Password</label>
                <div class="col-8">
                    <input class="form-control" type="password" name = "confirm_password" placeholder = "Password" required="true" >
                </div>
            </div>
            <div class="form-group row">
                <label id="contact_number" class="col-4 col-form-label">Contact number</label>
                <div class="col-8">
                    <input class="form-control" type="text" name = "phone_number" placeholder = "Contact number" required="true" >
                </div>
            </div>

            <input type = "submit" value = "Sign up" class="btn btn-primary">
        </form>
        {% if message %}
            {{ message }}
        {% endif %}
    </div>
    <br>
</body>

我的signup.html扩展了base.html

{% extends 'base.html' %}

{% block content %}
  <h2>Sign up</h2>
  <form method="post" action="{% url 'signup' %}">
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit">Sign up</button>
  </form>
{% endblock %}

3 个答案:

答案 0 :(得分:5)

UserCreationForm有三个字段:let screenSize: CGRect = UIScreen.mainScreen().bounds let screenWidth = screenSize.width let screenHeight = screenSize.height imageScrollView.frame = CGRect(x: 0, y: 0, width: viewWidth, height: screenHeight-50) usernamepassword1。您的表单无效,因为您已将密码字段更改为password2password

如果您在检查confirm_password后检查form.errors,则会发现form.is_valid()password1存在错误,因为模板中缺少这些错误。

答案 1 :(得分:3)

您正在尝试呈现HTML表单并将其传递给表单。您必须使用django模板渲染表单。像下面的东西

{{ form.as_p }}

这将使整个表单呈现在<p>标记

您还可以使用属性名称渲染单个字段。下面的代码将呈现一个html输入字段,该字段也可以包含<div>个标记。

{{form.email}}
{{form.first_name}}

等等。

您也可以查看working with forms

答案 2 :(得分:3)

您应该使用模板中的实际表单,而不是手动编写它。例如:

views.py

def signup(request):
    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if form.is_valid():
            form.save()
            return render(request, 'signup.html', {'message': 'Signed Up successfully'})

    else:
        form = SignUpForm()
    return render(request, 'signup.html', {form: form})

template.html

<form action="{% url 'signup' %}" method="post">
    {% csrf_token %}
    {{ form }}
    <input type="submit" value="Submit" />
</form>

我建议先尝试一下,检查它是否正常工作,然后担心样式和格式化。希望这有帮助!