表单在django中无效,因为它为空

时间:2018-07-19 20:30:56

标签: python django forms

我正在尝试为网站设置注册表单,但是由于该表单为空,因此打印不起作用,因此打印内容(request.POST)包含所有信息,但是该表单无效。

这是视图:

def register(request):
    print("Request submitted", request, request.method)
    if request.method == 'POST':
        print (request.POST)
        form = RegistrationForm(request.POST, prefix="user")
        print (form.is_valid())
        print (form.errors)
        if form.is_valid():
            ...
        else:
            print("not valid")

    else:
        form = RegistrationForm(prefix="user")
    return render(request, 'registration_form.html', {'form': form})

这是html:

<body>
    <div class="login">
        <h1>Alta de Usuario</h1>
        {% if messages %}
                {% for message in messages %}
                    <div class="alert">{{ message }}</div>
                {% endfor %}
        {% endif %}
        <form class="login-form" method="post">
            {% csrf_token %}
            <div>
                <label for="username">Nombre de usuario:</label>
                <input type="text" name="username" value="" placeholder="Elige un nombre de usuario">
                <p class="form-valid"><i class="fas fa-check"></i></p>
            </div>
            <div>
                <label for="email">eMail:</label>
                <input type="text" name="email" value="" placeholder="Introduce tu email">
                <p class="form-valid"><i class="fas fa-check"></i></p>
            </div>
            <div>
                <label for="pass">Contraseña:</label>
                <input type="password" name="pass" value="" placeholder="Introduce una contraseña">
                <p class="form-valid"><i class="fas fa-times"></i></p>
            </div>
            <div>
                <label for="pass">Repite contraseña:</label>
                <input type="password" name="pass" value="" placeholder="Repite la contraseña introducida">
                <p class="form-valid"><i class="fas fa-times"></i></p>
            </div>
            <div class="disclaimer">
                <div class="check-terms">
                    <input class="terms" name="terms" type="checkbox">
                    <label for="terms">
                        Acepto los <a href="">términos y condiciones</a>
                    </label>
                </div>
            </div>
            <button class="access" type="submit">Registrarme</button>
        </form>
    </div>
</body>
</html>

{#{{ form.as_p }}#}

非常感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

尝试像这样实例化表单:

moc_testheader.cpp

答案 1 :(得分:0)

已解决:

form = RegistrationForm(request.POST或无)