Home > Back-end >  user created with no password Django (users can't log in)
user created with no password Django (users can't log in)

Time:09-20

Im registering succesfully user, but when ot redirects me to login page I'm 100% sure password is correct, but it is not login me in, so I checked the admin page for that particular user, username exists, but it says that the password is not created yet.

this is my views.py

#from selectors import EpollSelector
from django.shortcuts import render, redirect
from django.contrib import messages
from django.http import HttpResponseRedirect
from .forms import UserRegisterForm, UserUpdateForm, ProfileUpdateForm
from django.contrib.auth.decorators import login_required
# Create your views here
 
def register(request):
    if request.method == "POST":
        form = UserRegisterForm(request.POST)
        if form.is_valid():
            form.save()
            username = form.cleaned_data.get("username")
            messages.success(request, f'You are now able to log in!')
            return redirect('login')
    else:  
        form = UserRegisterForm()
    return render(request, 'Users/register.html', {'form': form})
 

@login_required
def profile(request):
    if request.method == "POST":
        u_form = UserUpdateForm(request.POST, instance = request.user)
        p_form = ProfileUpdateForm(request.POST, 
                                   request.FILES, 
                                   instance=request.user.profile)
        if u_form.is_valid() and p_form.is_valid():
            u_form.save()
            p_form.save()
            messages.success(request, f'You account has been updated!')
            return redirect('profile')                           
    else:
        u_form = UserUpdateForm(instance = request.user)
        p_form = ProfileUpdateForm(instance=request.user.profile)
    
    context = {
        'u_form' :u_form, 
        'p_form' :p_form
    }
    return render(request, 'Users/profile.html', context)

and this is my forms.py code

from socket import fromshare
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserChangeForm
from .models import Profile
class UserRegisterForm(UserChangeForm):
    email = forms.EmailField()
    password1 = forms.CharField(label = "Password", widget = forms.PasswordInput)
    password2 = forms.CharField(label = "Repeat password", widget = forms.PasswordInput)

    
    class Meta:
        model = User
        fields = ['username', 'email', 'password1', 'password2']

class UserUpdateForm(forms.ModelForm):
    email = forms.EmailField()
    
    class Meta:
        model = User
        fields = ['username', 'email']

class ProfileUpdateForm(forms.ModelForm):
    class Meta:
        model = Profile
        fields = ['image']

Please suggest some solution. I was following Corey Schafer tutorial.

CodePudding user response:

Try to read about django.contrib.auth and passwords. https://docs.djangoproject.com/en/4.1/topics/auth/default/#changing-passwords

Django don't change password to hash, you should do it yourself before user.save()

in your case:

... # your staff
form = UserRegisterForm(request.POST)
    if form.is_valid():
        user = form.save(commit=False)
        user.set_password(form.cleaned_data['password'])
        user.save()
        username = user.username
        messages.success(request, f'You are now able to log in!')
... # other staff
  • Related