Skip to content

added password validators #250

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
29 changes: 29 additions & 0 deletions bootcamp/users/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.core.exceptions import ValidationError
from django.contrib.auth.password_validation import validate_password
from .models import *

class CustomUserCreationForm(UserCreationForm):
password1 = forms.CharField(
label="Password",
widget=forms.PasswordInput,
validators=[validate_password], # validates the password
)
password2 = forms.CharField(
label="Password confirmation",
widget=forms.PasswordInput,
help_text="Enter the same password as above, for verification.",
)

class Meta:
model = User
fields = ("username", "email")

def clean_password2(self):
# Checks that the two password entries match
password1 = self.cleaned_data.get("password1")
password2 = self.cleaned_data.get("password2")
if password1 and password2 and password1 != password2:
raise forms.ValidationError("Passwords do not match.")
return password2
2 changes: 1 addition & 1 deletion bootcamp/users/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.contrib.auth.models import AbstractUser
from django.contrib.auth.models import AbstractUser, UserManager
from django.contrib.auth.signals import user_logged_in, user_logged_out
from django.db import models
from django.urls import reverse
Expand Down
2 changes: 2 additions & 0 deletions bootcamp/users/urls.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.conf.urls import url
from django.urls import path

from . import views

Expand All @@ -12,4 +13,5 @@
view=views.UserDetailView.as_view(),
name="detail",
),
path('signup/',views.UserCreateView.as_view(), name="signup"),
]
15 changes: 14 additions & 1 deletion bootcamp/users/views.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
from django.contrib.auth.mixins import LoginRequiredMixin
from django.shortcuts import redirect
from django.urls import reverse
from django.views.generic import DetailView, ListView, RedirectView, UpdateView
from django.views.generic import DetailView, ListView, RedirectView, UpdateView, CreateView

from .models import User
from .forms import *
from django.contrib.auth import login

class UserCreateView(CreateView):
form_class = CustomUserCreationForm
template_name = "account/signup.html"

def form_valid(self, form):
# Log the user in upon successful registration
user = form.save()
login(self.request, user)
return redirect("users:detail", username=user.username)


class UserDetailView(LoginRequiredMixin, DetailView):
Expand Down