Authentication is not working for custom authentication

前端 未结 1 418
有刺的猬
有刺的猬 2021-01-29 15:12

I want to make custom login page in Django. I can successfully register user but when I want to login it did not get log in.

My views.py for login:

相关标签:
1条回答
  • 2021-01-29 15:41

    It seems you don't store any data in django.contrib.auth.models.User your should be something like this:

    models.py

    class Profile(models.Model):
        user = models.OneToOneField(User)
        contact = models.CharField(maX_length = 50)
        # User class contain other fields that you need.
    

    forms.py

    class UserForm(forms.Form):
        class Meta:
            model = User
    
    class ProfileForm(forms.Form):
        class Meta:
            model = Profile
    

    registeration code:

    def register(request):
        # ...
        # Your codes here
    
        profile_form = ProfileForm(data=request.POST)
    
        # Add this line:
        user_form = UserForm(data=request.POST)
    
        if user_form.is_valid() and profile_form.is_valid():
            user = user_form.save()
            user.set_password(user.password)
            user.save()
    
            profile = profile_form.save(commit=False)
            profile.save()
            registered = True
        else:
            print profile_form.errors
            print user_forms.errors
    else:
        profile_form = ProfileForm()
        user_form = UserForm()
    
    return render_to_response(
            'register.html',
            {'profile_form': profile_form, 'user_form':user_form 'registered': registered},
            context)
    

    You can find more Django official User fields here

    0 讨论(0)
提交回复
热议问题