I am following Antoinio Mele's -Django by example to build a Social Website but everythime I try to register a new user through the register.html template all I get is a blank form ,neither does the webiste create a new user.
I've followed everything that's said in the tutorial.
My views.py
from django.shortcuts import render
from django.http import HttpResponse
from django.shortcuts import render
from django.contrib.auth import authenticate, login
#from .forms import LoginForm,UserRegistrationForm
from .forms import LoginForm,UserRegistrationForm
from django.contrib.auth.decorators import login_required
def user_login(request):
if request.method == 'POST':
form = LoginForm(request.POST)
if form.is_valid():
cd = form.cleaned_data
user = authenticate(request,username=cd['username'],
password=cd['password'])
if user is not None:
if user.is_active:
login(request,user)
return HttpResponse('Authenticated successfully')
else:
return HttpResponse('Disabled account')
else:
return HttpResponse('Invalid login')
else:
form = LoginForm()
return render(request,'account/login.html',{'form':form})
@login_required
def dashboard(request):
return render(request,
'account/dashboard.html',
{'section':'dashboard'})
def register(request):
if request.method == 'POST':
user_form = UserRegistrationForm(request.POST)
if user_form.is_valid():
# Create a new user object but avoid saving it yet
#new_user = user_form.save(commit=False)
new_user = user_form.save()
# Set the chosen password
new_user.set_password(
user_form.cleaned_data['password'])
# Save the User object
new_user.save()
# Create the user profile
# Profile.objects.create(user=new_user)
return render(request,
'account/register_done.html',
{'new_user': new_user})
else:
user_form = UserRegistrationForm()
return render(request,
'account/register.html',
{'user_form': user_form})
My urls.py
from django.urls import path
from django.contrib.auth import views as auth_views
from . import views
urlpatterns=[
#path('login/',views.user_login,name='login'),
path('login/',auth_views.LoginView.as_view(),name='login'),
path('logout/',auth_views.LogoutView.as_view(),name='logout'),
path('',views.dashboard,name='dashboard'),
path('register/',views.register,name='register'),
]
my register.html
{% extends 'account/base.html' %}
{% block title %}
Create an account
{% endblock title %}
{% block content %}
<h1>Create an account</h1>
<p >
Please,sign up using the following form:
</p>
<form mehtod='post'>
{{ user_form.as_p }}
{% csrf_token %} <!--cross site request frogery-->
<p><input type="submit" value="Create my account"></p>
</form>
{% endblock content %}
my forms.py
from django.contrib.auth.models import User
from django import forms
class LoginForm(forms.Form):
username = forms.CharField()
password = forms.CharField(widget=forms.PasswordInput)
class UserRegistrationForm(forms.ModelForm):
password = forms.CharField(label='Password',
widget=forms.PasswordInput)
password2 = forms.CharField(label='Repeat password',
widget=forms.PasswordInput)
class Meta:
model = User
fields = ('username', 'first_name', 'email')
def clean_password2(self):
cd = self.cleaned_data
if cd['password'] != cd['password2']:
raise forms.ValidationError('Passwords don\'t match.')
return cd['password2']
Any little assistance would be greatly appreciated.
CodePudding user response:
Try using the code below, you can print form data to trace its status
# Create a new user object but avoid saving it yet
# create new instance of user
new_user = user_form.save(commit=False)
# Set the chosen password
new_user.password = user_form.cleaned_data['password'])
# Save the User object
new_user.save()
CodePudding user response:
You have an error in your html, <form mehtod='post'>
should be <form method='post'>
so the form is being sent as a GET request instead.