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

[feature request] Adicionar view de registro #54

Merged
merged 4 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions apps/users/forms.py
Original file line number Diff line number Diff line change
@@ -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"]
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("", 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:
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})
1 change: 1 addition & 0 deletions server/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 @@ -70,7 +70,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 @@ -80,6 +80,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