From 8349d9ff26d9283f773a3f5d22687acc0e26563a Mon Sep 17 00:00:00 2001 From: Piotr Kopycki Date: Wed, 15 Dec 2021 16:46:50 +0100 Subject: [PATCH] Registration page linked with backend --- templates/register.html | 23 +++++++++++------------ templates/registerSuccess.html | 9 +++++++++ users/forms.py | 14 ++++++++++++++ users/urls.py | 10 ++++++---- users/views.py | 25 +++++++++++++++++++++---- 5 files changed, 61 insertions(+), 20 deletions(-) create mode 100644 templates/registerSuccess.html create mode 100644 users/forms.py diff --git a/templates/register.html b/templates/register.html index a5a39b6..1799eb8 100644 --- a/templates/register.html +++ b/templates/register.html @@ -4,19 +4,18 @@ {% block content %}

Register

-
-

First Name

- -

Last Name

- -

Email

- -

Password

- -

Repeat password

- + {% csrf_token %} + + {% for field in registration_form %} +

+ {{field.label_tag}} + {{field}} + {% for error in field.errors %} +

{{error}}

+ {% endfor %} +

+ {% endfor %} -

Remember Me

{% endblock %} diff --git a/templates/registerSuccess.html b/templates/registerSuccess.html new file mode 100644 index 0000000..2c66dc4 --- /dev/null +++ b/templates/registerSuccess.html @@ -0,0 +1,9 @@ +{% extends "authBase.html" %} + +{% block title %}Log Out{% endblock %} + +{% block content %} +

Account have been created successfully. Now you can log in

+ Log in now +{% endblock %} + diff --git a/users/forms.py b/users/forms.py new file mode 100644 index 0000000..fdefaaa --- /dev/null +++ b/users/forms.py @@ -0,0 +1,14 @@ +from django import forms +from django.contrib.auth.forms import UserCreationForm +from .models import User + +class RegistrationForm(UserCreationForm): + email = forms.EmailField(max_length=60, widget=forms.EmailInput(attrs={'placeholder': 'Enter Email'})) + first_name = forms.CharField(max_length=60, widget=forms.TextInput(attrs={'placeholder': 'Enter First Name'})) + last_name = forms.CharField(max_length=60, widget=forms.TextInput(attrs={'placeholder': 'Enter Last Name'})) + password1 = forms.CharField(widget=forms.PasswordInput(attrs={'placeholder': 'Enter Password'})) + password2 = forms.CharField(widget=forms.PasswordInput(attrs={'placeholder': 'Repeat Password'})) + + class Meta: + model = User + fields = ("email", "first_name", "last_name", "password1", "password2") \ No newline at end of file diff --git a/users/urls.py b/users/urls.py index f0097f2..1c75f05 100644 --- a/users/urls.py +++ b/users/urls.py @@ -4,7 +4,7 @@ from django.urls import path from users.views import UserModelViewSet from rest_framework_simplejwt.views import TokenObtainPairView from rest_framework_simplejwt.views import TokenRefreshView -from .views import PasswordReset, UserPasswordResetConfirmView, RegisterViewSet, login, logout, register +from .views import PasswordReset, UserPasswordResetConfirmView, RegisterViewSet, login, logout, register, register_success router = DefaultRouter(trailing_slash=False) @@ -13,9 +13,11 @@ router.register("items", UserModelViewSet) urlpatterns = [ path("", include(router.urls)), - path('login', login, name='register'), - path('logout', logout, name='register'), - path('register', RegisterViewSet.as_view(), name='register'), + path('login', login, name='login'), + path('logout', logout, name='logout'), + path('register/success', register_success, name='register_success'), + # path('register', RegisterViewSet.as_view(), name='register'), + path('register', register, name='register'), path('api/token', TokenObtainPairView.as_view(), name='token_obtain_pair'), path('api/token/refresh', TokenRefreshView.as_view(), name='token_refresh'), path("password/reset", PasswordReset.as_view()), diff --git a/users/views.py b/users/views.py index 573af3f..b62d505 100644 --- a/users/views.py +++ b/users/views.py @@ -9,6 +9,7 @@ from .serializers import UserPasswordResetSerializer, UserPasswordResetConfirmSe from rest_framework.response import Response from django.shortcuts import render, redirect from django.template import loader +from .forms import RegistrationForm @@ -77,8 +78,24 @@ def logout(request): # } return render(request, 'logout.html') +def register_success(request): + return render(request, 'registerSuccess.html') + def register(request): - # context = { - # 'latest_question_list': latest_question_list, - # } - return render(request, 'register.html') + context = {} + if request.POST: + form = RegistrationForm(request.POST) + if form.is_valid(): + User.objects.create( + email=form.cleaned_data["email"], + first_name=form.cleaned_data["first_name"], + last_name=form.cleaned_data["last_name"], + password=form.cleaned_data["password1"] + ) + return redirect('register_success') + else: + context['registration_form'] = form + else: + form = RegistrationForm() + context['registration_form'] = form + return render(request, 'register.html', context)