From 5afac5e2ab75fa10d76936aa5aaade4fdcc653d8 Mon Sep 17 00:00:00 2001 From: HladczukLe Date: Tue, 31 Oct 2023 00:28:01 -0300 Subject: [PATCH 1/3] feat(registerView): add register view --- apps/users/forms.py | 9 +++++++ apps/users/urls.py | 9 +++++++ apps/users/views.py | 21 +++++++++++++++ server/settings/base.py | 2 ++ server/urls.py | 1 + templates/base.html | 3 ++- templates/registration/register.html | 39 +++++----------------------- 7 files changed, 51 insertions(+), 33 deletions(-) create mode 100644 apps/users/forms.py create mode 100644 apps/users/urls.py create mode 100644 apps/users/views.py diff --git a/apps/users/forms.py b/apps/users/forms.py new file mode 100644 index 0000000..ba23570 --- /dev/null +++ b/apps/users/forms.py @@ -0,0 +1,9 @@ +from django.contrib.auth.forms import UserCreationForm + +from apps.users.models import User + + +class CreateUserForm(UserCreationForm[User]): + class Meta: + model = User + fields = ["username", "email", "password1", "password2"] diff --git a/apps/users/urls.py b/apps/users/urls.py new file mode 100644 index 0000000..523a9aa --- /dev/null +++ b/apps/users/urls.py @@ -0,0 +1,9 @@ +from django.urls import path + +from apps.users.views import RegisterView + +app_name = "users" + +urlpatterns = [ + path("register/", RegisterView.as_view(), name="register"), +] diff --git a/apps/users/views.py b/apps/users/views.py new file mode 100644 index 0000000..35340d4 --- /dev/null +++ b/apps/users/views.py @@ -0,0 +1,21 @@ +from django.http import HttpRequest, HttpResponse +from django.shortcuts import redirect, render +from django.views import View + +from apps.users.forms import CreateUserForm + + +class RegisterView(View): + template_name = "registration/register.html" + + def get(self, request: HttpRequest) -> HttpResponse | None: + form = CreateUserForm() + return render(request, self.template_name, {"form": form}) + + def post(self, request: HttpRequest) -> HttpResponse | None: + form = CreateUserForm(self.request.POST) + if form.is_valid(): + form.save() + return redirect("login") + + return render(request, self.template_name, {"form": form}) diff --git a/server/settings/base.py b/server/settings/base.py index d6dacd4..0a72994 100644 --- a/server/settings/base.py +++ b/server/settings/base.py @@ -142,6 +142,8 @@ LOGIN_URL = "login" +REGISTER_URL = "register" + # The model to use to represent an user. # https://docs.djangoproject.com/en/4.1/ref/settings/#auth-user-model diff --git a/server/urls.py b/server/urls.py index 58d3041..38a11bc 100644 --- a/server/urls.py +++ b/server/urls.py @@ -11,4 +11,5 @@ path("", IndexView.as_view(), name="home"), path("contests/", include("apps.contests.urls")), path("problems/", include("apps.problems.urls")), + path("register/", include("apps.users.urls")), ] diff --git a/templates/base.html b/templates/base.html index 1ff2c39..7d2f214 100644 --- a/templates/base.html +++ b/templates/base.html @@ -63,7 +63,7 @@ {% endif %} -
  • You profile
  • +
  • Your profile
  • @@ -73,6 +73,7 @@ {% else %} + Register Sign in {% endif %} diff --git a/templates/registration/register.html b/templates/registration/register.html index fdedbcc..13066fb 100644 --- a/templates/registration/register.html +++ b/templates/registration/register.html @@ -9,39 +9,14 @@
    Register
    -
    + {% csrf_token %} -
    - - -
    -
    - - -
    -
    - - -
    - + {% for field in form%} + {{field.label}} + {{field}} + {% endfor %} + + {{form.errors}}
    From c57f0c1a5df450abb92686e22f2c80c62ca2cd51 Mon Sep 17 00:00:00 2001 From: HladczukLe Date: Tue, 31 Oct 2023 00:47:28 -0300 Subject: [PATCH 2/3] feat(registerView): add register view --- apps/users/forms.py | 9 ++++++++- apps/users/urls.py | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/apps/users/forms.py b/apps/users/forms.py index ba23570..84799c2 100644 --- a/apps/users/forms.py +++ b/apps/users/forms.py @@ -1,9 +1,16 @@ +from typing import TYPE_CHECKING + from django.contrib.auth.forms import UserCreationForm from apps.users.models import User +if TYPE_CHECKING: + BaseUserCreationForm = UserCreationForm[User] +else: + BaseUserCreationForm = UserCreationForm + -class CreateUserForm(UserCreationForm[User]): +class CreateUserForm(BaseUserCreationForm): class Meta: model = User fields = ["username", "email", "password1", "password2"] diff --git a/apps/users/urls.py b/apps/users/urls.py index 523a9aa..a3d45bc 100644 --- a/apps/users/urls.py +++ b/apps/users/urls.py @@ -5,5 +5,5 @@ app_name = "users" urlpatterns = [ - path("register/", RegisterView.as_view(), name="register"), + path("", RegisterView.as_view(), name="register"), ] From b76c212971187f81ff07b96257fa775c09b1a215 Mon Sep 17 00:00:00 2001 From: HladczukLe Date: Wed, 1 Nov 2023 15:31:27 -0300 Subject: [PATCH 3/3] fix(registerView): fix functions get and post --- apps/users/views.py | 4 ++-- server/settings/base.py | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/apps/users/views.py b/apps/users/views.py index 35340d4..012556e 100644 --- a/apps/users/views.py +++ b/apps/users/views.py @@ -8,11 +8,11 @@ class RegisterView(View): template_name = "registration/register.html" - def get(self, request: HttpRequest) -> HttpResponse | None: + def get(self, request: HttpRequest) -> HttpResponse: form = CreateUserForm() return render(request, self.template_name, {"form": form}) - def post(self, request: HttpRequest) -> HttpResponse | None: + def post(self, request: HttpRequest) -> HttpResponse: form = CreateUserForm(self.request.POST) if form.is_valid(): form.save() diff --git a/server/settings/base.py b/server/settings/base.py index 0a72994..b0fceaa 100644 --- a/server/settings/base.py +++ b/server/settings/base.py @@ -141,7 +141,6 @@ LOGOUT_REDIRECT_URL = "home" LOGIN_URL = "login" - REGISTER_URL = "register" # The model to use to represent an user.