简体   繁体   中英

Django 1.9 form.is_valid() returns false

I'm trying to auth the username and password from django's own USER model. The POST Request works, is there any thing wrong with the forms.py?

views.py

from django.shortcuts import render,get_object_or_404
from django.http import HttpResponse
from .forms import LoginForm
from django.contrib.auth import authenticate, login as auth_login

def login(request):
    msg = "Login"
    if (request.method == "POST"):

        form = LoginForm(request.POST or None)
        msg = "post"
        if form.is_valid():
            msg = "valid"

    context = {
        "msg":msg,
    }

    return render(request, "login.html", context)

forms.py

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

class LoginForm(forms.ModelForm):
    class Meta:
        model = User
        fields = [
            "username",
            "password"
        ]

Login.html

<div class="container">
        {% if user.is_authenticated %}
            blablabla
        {% else %}
        <div class="row">
            <div class="col-md-6" style="background-color: lightblue">
                <h1 style="color:red">{{ msg }}</h1>
                <form method="POST" action="">{% csrf_token %} 
                    acronym<input type="acronym" name="acronym">
                    password<input type="password" name="password">
                <input type="submit" value="Login">
                </form>
            </div>
        {% endif %}

Edit added the login.html file

The form isn't in your context, neither is it instantiated for a get request.

To start with try something like so;

def login(request):
    msg = "Login"
    form = LoginForm()

    if (request.method == "POST"):

        form = LoginForm(request.POST or None)
        msg = "post"
        if form.is_valid():
            msg = "valid"

    context = {
        "form": form,
        "msg":msg,
    }

    return render(request, "login.html", context)

That way you can check {{ form.errors }} in your template to see what's wrong.

At the moment how do you render the form in login.html ? Are you using a loop over fields in the form like this;

<form class="form" action="" method="POST">

    {% if form.non_field_errors or form.errors %}
        <div class="error-list-block">
            <p>
                Please correct the errors below - Ensure all required fields
                are completed.
            </p>
            <ul>
                {% for error in form.non_field_errors|add:form.errors %}
                    <li>{{ error }}</li>
                {% endfor %}
            </ul>
        </div>
    {% endif %}

    {% csrf_token %}
    {% for field in form %}
        {% if field.is_hidden %}
            {{ field }}
        {% else %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM