简体   繁体   中英

why Django form.is_valid() always false

I'm writing a login code. but form.is_valid() always false I don't know why?

model.py

class User(models.Model):
    Man = 'M'
    Woman = 'W'
    GENDER_CHOICES=(
        (Man, 'M'),
        (Woman, 'W'),
    )
    user_no = models.AutoField(primary_key=True)
    user_email = models.EmailField(max_length=254)
    user_pw = models.CharField(max_length=50)
    user_gender = models.CharField(
        max_length=1,
        choices=GENDER_CHOICES,
        default=Man,
    )
    user_birthday = models.DateField(blank=True)
    user_jdate = models.DateTimeField(auto_now_add=True)

signin.html

<form method="post" action="{% url 'signin' %}">
{% csrf_token %}
<h3>ID : {{form.user_email}} </h3>
<h3>PASSWORD : {{form.user_pw}} </h3>


<input type="submit" class="btn_submit" value="로그인" />

views.py

def signin(request):
if request.method == 'POST':
    form = Form(request.POST)
    if form.is_valid():
        print("success")
    else:
        print("false")
else:
    form = Form()
return render(request,'signin.html',{'form':form})

1) What's wrong?

2)The other signups are true because the signup_bails are true, but why is the signin always false?

3)How do I fix it?

I'd suggest a couple of things. First of, you need to define your Form Object first. If you did and you are importing this form, then please submit it here to review.

If you're interested in creating your own User Model, why don't you extend Django's User model. It already comes with all features like login, groups and permissions. Maybe you'd like other properties besides the Django User's Model. You can add this attributes within your extended model.

app/models.py

from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, UserManager, AbstractUser


class User(AbstractUser):

    def __init__(self, *args, **kwargs):
        super(User, self).__init__(*args, **kwargs)
        is_active = models.BooleanField(default=True)
        is_admin = models.BooleanField(default=False)
        is_staff = models.BooleanField(default=False)

    def get_short_name(self):
        return self.username

    def __str__(self):
        if self.first_name and self.last_name:
            name = self.first_name + " " + self.last_name
        else:
            name = self.username
        return name

    def __unicode__(self):
        return self.email

In this example you can see I've extended my custom 'User' Model to AbstractUser from django.contrib.auth.models, this means I can use all attributes from Django's User model but also I added three more fields (is_active, is_admin, is_staff).

Dont forget to register this new extended Model to settings.py

settings.py

 AUTH_USER_MODEL = 'app.User'

Now let say you'd like to update User instances from your User Model. Let's use a Form that its based on this User model.

app/forms.py

from django import forms 
from .models import User


class UpdateUserForm(forms.ModelForm):

    class Meta():
        model = User

Now to lets use this form in your views.py

app/views.py

from .models import User
from .forms import UpdateUserForm
from django.shortcuts import get_object_or_404


def update_user(request, id):
    _user = get_object_or_404(User, id=id)
    form = UpdateUserForm(instance=_user)
    template = 'app/template.html'
    if request.method == 'POST':
       form = UpdateUserForm(request.POST, instance=_user)
       if form.is_valid():
           form.save()
    context = {
        'form':form
    }
    return render(request, template, context)

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