Registration page linked with backend

This commit is contained in:
Piotr Kopycki 2021-12-15 16:46:50 +01:00
parent e78586bbd6
commit 8349d9ff26
5 changed files with 61 additions and 20 deletions

View File

@ -4,19 +4,18 @@
{% block content %} {% block content %}
<h2>Register</h2> <h2>Register</h2>
<form action=""> <form method="post"> {% csrf_token %}
<p>First Name</p>
<input type="text" placeholder="Enter First Name"> {% for field in registration_form %}
<p>Last Name</p> <p>
<input type="text" placeholder="Enter Last Name"> {{field.label_tag}}
<p>Email</p> {{field}}
<input type="email" placeholder="Enter Email"> {% for error in field.errors %}
<p>Password</p> <p style="color: red;">{{error}}</p>
<input type="password" placeholder="Enter Password"> {% endfor %}
<p>Repeat password</p> </p>
<input type="password" placeholder="Enter Password"> {% endfor %}
<input type="submit" value="Crete account"> <input type="submit" value="Crete account">
<p><input type="checkbox">Remember Me</p>
</form> </form>
{% endblock %} {% endblock %}

View File

@ -0,0 +1,9 @@
{% extends "authBase.html" %}
{% block title %}Log Out{% endblock %}
{% block content %}
<p>Account have been created successfully. Now you can log in</p>
<a href="{% url 'login' %}">Log in now</a>
{% endblock %}

14
users/forms.py Normal file
View File

@ -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")

View File

@ -4,7 +4,7 @@ from django.urls import path
from users.views import UserModelViewSet from users.views import UserModelViewSet
from rest_framework_simplejwt.views import TokenObtainPairView from rest_framework_simplejwt.views import TokenObtainPairView
from rest_framework_simplejwt.views import TokenRefreshView 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) router = DefaultRouter(trailing_slash=False)
@ -13,9 +13,11 @@ router.register("items", UserModelViewSet)
urlpatterns = [ urlpatterns = [
path("", include(router.urls)), path("", include(router.urls)),
path('login', login, name='register'), path('login', login, name='login'),
path('logout', logout, name='register'), path('logout', logout, name='logout'),
path('register', RegisterViewSet.as_view(), name='register'), 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', TokenObtainPairView.as_view(), name='token_obtain_pair'),
path('api/token/refresh', TokenRefreshView.as_view(), name='token_refresh'), path('api/token/refresh', TokenRefreshView.as_view(), name='token_refresh'),
path("password/reset", PasswordReset.as_view()), path("password/reset", PasswordReset.as_view()),

View File

@ -9,6 +9,7 @@ from .serializers import UserPasswordResetSerializer, UserPasswordResetConfirmSe
from rest_framework.response import Response from rest_framework.response import Response
from django.shortcuts import render, redirect from django.shortcuts import render, redirect
from django.template import loader from django.template import loader
from .forms import RegistrationForm
@ -77,8 +78,24 @@ def logout(request):
# } # }
return render(request, 'logout.html') return render(request, 'logout.html')
def register_success(request):
return render(request, 'registerSuccess.html')
def register(request): def register(request):
# context = { context = {}
# 'latest_question_list': latest_question_list, if request.POST:
# } form = RegistrationForm(request.POST)
return render(request, 'register.html') 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)