Skip to content
This repository has been archived by the owner on Jan 3, 2024. It is now read-only.

Commit

Permalink
feat(registerView): add register view
Browse files Browse the repository at this point in the history
  • Loading branch information
HladczukLe committed Oct 31, 2023
1 parent e50cbe4 commit 5afac5e
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 33 deletions.
9 changes: 9 additions & 0 deletions apps/users/forms.py
Original file line number Diff line number Diff line change
@@ -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"]
9 changes: 9 additions & 0 deletions apps/users/urls.py
Original file line number Diff line number Diff line change
@@ -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"),
]
21 changes: 21 additions & 0 deletions apps/users/views.py
Original file line number Diff line number Diff line change
@@ -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})
2 changes: 2 additions & 0 deletions server/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions server/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
]
3 changes: 2 additions & 1 deletion templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
</a>
</li>
{% endif %}
<li><a href="#" class="dropdown-item">You profile</a></li>
<li><a href="#" class="dropdown-item">Your profile</a></li>
<li><hr class="dropdown-divider"></li>
<li>
<a href="{% url 'logout' %}" class="dropdown-item">
Expand All @@ -73,6 +73,7 @@
</ul>
</div>
{% else %}
<a href="{% url 'users:register' %}" class="nav-link text-white">Register</a>
<a href="{% url 'login' %}" class="nav-link text-white">Sign in</a>
{% endif %}
</div>
Expand Down
39 changes: 7 additions & 32 deletions templates/registration/register.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,14 @@
<div class="card">
<div class="card-header">Register</div>
<div class="card-body">
<form method="POST" action="{% url 'register' %}">
<form method="POST" action="{% url 'users:register' %}">
{% csrf_token %}
<div class="mb-3">
<label for="name" class="form-label">Name</label>
<input
type="text"
class="form-control"
id="name"
name="name"
required
>
</div>
<div class="mb-3">
<label for="email" class="form-label">Email</label>
<input
type="email"
class="form-control"
id="email"
name="email"
required
>
</div>
<div class="mb-3">
<label for="password" class="form-label">Password</label>
<input
type="password"
class="form-control"
id="password"
name="password"
required
>
</div>
<button type="submit" class="btn btn-primary">Register</button>
{% for field in form%}
{{field.label}}
{{field}}
{% endfor %}
<button type="submit" class="btn btn-primary">Register</button>
{{form.errors}}
</form>
</div>
</div>
Expand Down

0 comments on commit 5afac5e

Please sign in to comment.