user8504021
user8504021

Reputation: 283

Password is not saved

The password is not saved in new user registration. I wrote in views.py

def regist(request):
    regist_form = RegisterForm(request.POST or None)
    context = {
        'regist_form': regist_form,
    }
    return render(request, 'registration/regist.html', context)

@require_POST
def regist_save(request):
    regist_form = RegisterForm(request.POST)
    if regist_form.is_valid():
        user = regist_form.save(commit=False)
        password = regist_form.cleaned_data.get('password')
        user.set_password(password)
        user.save()
        login(request, user)
        context = {
            'user': request.user,
        }
        return redirect('detail')

    context = {
        'regist_form': regist_form,
    }
    return render(request, 'registration/regist.html', context)

in regist.html

<div class="heading col-lg-6 col-md-12">
            
        <h2>New account registration</h2>
    
        <form class="form-horizontal" action="regist_save/" method="POST">
            
          <div class="form-group-lg">
            
            <label for="id_username">username</label>
            
            {{ regist_form.username }}
            
          </div>
            
            
          <div class="form-group-lg">
            
            <label for="id_email">email</label>
            
             {{ regist_form.email }}
          </div>   
          <div class="form-group-lg">
            
            <label for="id_password">password</label>
            
             {{ regist_form.password1 }}
            
          </div>
            
            
          <div class="form-group-lg">
            <label for="id_password">password(conformation)</label>
            
             {{ regist_form.password2 }}
             <p class="help-block">{{ regist_form.password2.help_text }}</p>
            
          </div>
            
            
          <div class="form-group-lg">
            <div class="col-xs-offset-2">
            <button type="submit" class="btn btn-primary btn-lg">SUBMIT</button>
            
            <input name="next" type="hidden"/>
            
            </div>
          </div>
          {% csrf_token %}
            
        </form>
            
</div>

in forms.py

class RegisterForm(UserCreationForm):
    class Meta:
        model = User
        fields = ('username', 'email','password1','password1',)
    def __init__(self, *args, **kwargs):
        super(RegisterForm, self).__init__(*args, **kwargs)
        self.fields['username'].widget.attrs['class'] = 'form-control'
        self.fields['email'].widget.attrs['class'] = 'form-control'
        self.fields['password1'].widget.attrs['class'] = 'form-control'
        self.fields['password2'].widget.attrs['class'] = 'form-control'

Username & email is registered normally. So I really cannot understand why the password is not registered. I read Django tutorial and I think my code is ok.

How should I fix this? What should I write it?

Upvotes: 1

Views: 59

Answers (1)

neverwalkaloner
neverwalkaloner

Reputation: 47374

Your form have fields password1 and password2 and not password and regist_form.cleaned_data.get('password') return None. So you should rename password1 field to password:

class RegisterForm(UserCreationForm):
    class Meta:
        model = User
        fields = ('username', 'email','password', 'password2',)

Upvotes: 2

Related Questions