diff --git a/apps/users/forms.py b/apps/users/forms.py new file mode 100644 index 0000000..84799c2 --- /dev/null +++ b/apps/users/forms.py @@ -0,0 +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(BaseUserCreationForm): + 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..a3d45bc --- /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("", RegisterView.as_view(), name="register"), +] diff --git a/apps/users/views.py b/apps/users/views.py new file mode 100644 index 0000000..012556e --- /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: + form = CreateUserForm() + return render(request, self.template_name, {"form": form}) + + def post(self, request: HttpRequest) -> HttpResponse: + 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..b0fceaa 100644 --- a/server/settings/base.py +++ b/server/settings/base.py @@ -141,6 +141,7 @@ LOGOUT_REDIRECT_URL = "home" 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 d8d75b9..acd146e 100644 --- a/templates/base.html +++ b/templates/base.html @@ -70,7 +70,7 @@ {% endif %} -