python - My registration view is not populating the user table in Django -
i have been trying login , register on sane template, have been having issues it. please need help. below codes:
views.py:
def register_user(request): if request.user.is_authenticated(): homecoming redirect('home') if request.method == 'post': rform = registrationform(request.post) if rform.is_valid(): user = user.objects.create_user() user.username = rform.cleaned_data['email'] user.set_password(rform.cleaned_data['password']) user.first_name = rform.cleaned_data['first_name'] user.last_name = rform.cleaned_data['last_name'] user.email = rform.cleaned_data['email'] user.gender = rform.cleaned_data['gender'] user.save() loggedin_user = authenticate(email = rform.cleaned_data['email'], password = rform.cleaned_data['password']) if user not none: login(request, loggedin_user) homecoming redirect('home') else: homecoming render(request, 'accounts/access.html', {'rform': registrationform()}) else: homecoming render(request, 'accounts/access.html', {'rform': registrationform()}) else: form = registrationform() homecoming render(request, 'accounts/access.html', {'rform':form}) def login_now(request, *args, **kwargs): if request.user.is_authenticated(): homecoming redirect('home') if request.method == "post": form = authenticationform(request.post) if form.is_valid(): username = form.cleaned_data['email'] password = form.cleaned_data['password'] user = authenticate(username = form.cleaned_data['email'], password = password) if user not none: login(request, user) homecoming redirect('home') else: homecoming render(request, 'accounts/access.html', {'form': authenticationform(), 'rform':registrationform(), 'next':reverse_lazy('home')}) else: homecoming render(request, 'accounts/access.html', {'form': authenticationform(), 'rform':registrationform(), 'next':reverse_lazy('home')}) else: homecoming render(request, 'accounts/access.html', {'form': authenticationform(), 'rform':registrationform(), 'next':reverse_lazy('home')})
forms.py:
choices = [ ('male', "male"), ('female', "female"), ] class registrationform(forms.form): first_name = forms.charfield(max_length=25, widget=forms.textinput(attrs={'placeholder': 'first name'})) last_name = forms.charfield(max_length=25, widget=forms.textinput(attrs={'placeholder': 'last name'})) email = forms.emailfield(max_length=50, widget=forms.textinput(attrs={'placeholder': 'email'})) password = forms.charfield(max_length=25, widget=forms.passwordinput(attrs={'placeholder': 'password'})) password1 = forms.charfield(max_length=25, widget=forms.passwordinput(attrs={'placeholder': 'password confirm'}), label=("re-type password")) gender = forms.choicefield(choices=choices, widget=forms.radioselect(attrs={'placeholder': 'gender'})) class meta: model = ('user',) def clean_email(self): info = self.cleaned_data['email'] if user.objects.get(email=data): raise forms.validationerror('a user email exist. may recover password password reset') homecoming info def clean_password(self): password = self.cleaned_data.get("password") password1 = self.cleaned_data.get("password1") if password1 , password , password1 != password: raise forms.validationerror( self.error_messages['password_mismatch'], code='password_mismatch', ) homecoming password
access.html:
<div id = 'signup'> <form id="post-form" action="{% url 'register' %}" method="post"> {% csrf_token %} <h3>register</h3> <div> {%for field in rform%} <div style="margin-top:10px;"> {{field.label_tag}}<br/> {{field}} {%if field.errors%} <br/>{{field.errors}} {%endif%} </div> {%endfor%} </div> <input type="submit" value="register" class='sub' id='register'/> </div> <div id='login'> {% if form.errors %} {{ form.non_field_errors}} {% endif %} <form action='{% url 'login' %}' method='post' id ='signin'> {% csrf_token %} <h3>sign in</h3> <p><label>email:</label><br/> {{ rform.email }} {{ rform.email.errors }}</p> <p><label>password:</label><br/> {{ rform.password }} {{ rform.password.errors }}</p> <p><input type="submit" value="login" id='submit' class='sub' /><br> <input type="hidden" name="next" value="{% url 'home' %}" /> </form> </div>
i seek register above , get, redirection, please need help. in advance.
please note, extended user profile that's why have gender in registration form.
in code, not comparing right parameters
loggedin_user = authenticate(email = rform.cleaned_data['email'], password = rform.cleaned_data['password']) if user not none: login(request, loggedin_user) homecoming redirect('home')
you checking none variable, not 1 returned authenticate ! if loggedin_user not none
right check.
check illustration on official doc.
you need send validated form rform
can show feedback (errors) user, sending new form after validation registrationform()
!
also seek identify source of problem, posting hole code don't know problem comes from, @ to the lowest degree debugging ;) !
python django
No comments:
Post a Comment