I am new to django and I have create a sign up form and its working but when I try to validate password fields to see the if password1 and password2 is matched then I get error. For instance, if my password1=12345678 and password1= TY123456 then I get an error "The view website.views.signUp didn't return an HttpResponse object. It returned None instead". Basically, I just want to dipslay error instead of redirecting to error page. Can anyone help me please? How to do server side validation of signup form.Many Thanks.
view.py
def signUp(request):
form = UserCreationForm()
if request.method == 'POST':
filled_form = UserCreationForm(request.POST)
if filled_form.is_valid():
password1 = filled_form.cleaned_data['password1']
password2 = filled_form.cleaned_data['password2']
if password1 != password2:
note='You password does not match'
return render(request,'signup.html',{'form':form, 'note':note})
else:
filled_form.save()
return redirect('login')
else:
return render(request,'signup.html',{'form':form})
signup.html
<h1>Sign Up</h1>
<span style="color: red;">{{note}}</span>
<form method="POST">
{% csrf_token %}
{{form.as_p}}
<button type="submit">Sign Up</button>
</form>
urls.py
from django.contrib import admin
from django.urls import path, include
from website.views import home,signUp
urlpatterns = [
path('admin/', admin.site.urls),
path('accounts/',include('django.contrib.auth.urls')),
path('signup/',signUp, name='signup'),
path('',home,name='home')
]