diff --git a/mysite/settings.py b/mysite/settings.py index 572a80b..f5523db 100644 --- a/mysite/settings.py +++ b/mysite/settings.py @@ -126,3 +126,9 @@ STATICFILES_DIRS = [BASE_DIR] # https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' + +LOGIN_REDIRECT_URL = "home_redirect" +LOGOUT_REDIRECT_URL = "home_redirect" + +EMAIL_BACKEND = "django.core.mail.backends.filebased.EmailBackend" +EMAIL_FILE_PATH = BASE_DIR / "sent_emails" \ No newline at end of file diff --git a/mysite/urls.py b/mysite/urls.py index 1b67d3f..5e952b1 100644 --- a/mysite/urls.py +++ b/mysite/urls.py @@ -7,4 +7,5 @@ urlpatterns = [ path('', RedirectView.as_view(pattern_name='polls:index'), name='home_redirect'), path("polls/", include("polls.urls")), path('admin/', admin.site.urls), + path("accounts/", include("django.contrib.auth.urls")), ] diff --git a/polls/forms.py b/polls/forms.py new file mode 100644 index 0000000..1e738ea --- /dev/null +++ b/polls/forms.py @@ -0,0 +1,27 @@ +from django import forms +from django.contrib.auth.forms import UserCreationForm +from django.contrib.auth.models import User + + +class SignUpForm(UserCreationForm): + tailwind_class = "w-full border-2 border-gray-300 bg-gray-100 rounded-lg focus:ring focus:border-blue-300 focus:shadow-none" + + username = forms.CharField(widget=forms.TextInput(attrs={'class': tailwind_class}), + error_messages={ + 'unique': 'This username is already in use.', + 'invalid': 'Invalid username format.', + 'max_length': 'Username should not exceed 150 characters.', + } + ) + password1 = forms.CharField(widget=forms.PasswordInput(attrs={'class': tailwind_class}), + error_messages={'min_length': 'Password must contain at least 8 characters.',} + ) + password2 = forms.CharField(widget=forms.PasswordInput(attrs={'class': tailwind_class}),) + + class Meta: + model = User + fields = ('username', 'password1', 'password2') + + error_messages = { + 'password_mismatch': "The two password fields didn't match.", + } \ No newline at end of file diff --git a/polls/models.py b/polls/models.py index 1e06a4e..24d3eb2 100644 --- a/polls/models.py +++ b/polls/models.py @@ -14,6 +14,7 @@ from django.utils import timezone from django.contrib import admin from django.core.validators import MaxValueValidator, MinValueValidator from django.db.models import Sum +from django.contrib.auth.models import User class Question(models.Model): diff --git a/polls/templates/polls/index.html b/polls/templates/polls/index.html index ff23cdc..3451240 100644 --- a/polls/templates/polls/index.html +++ b/polls/templates/polls/index.html @@ -26,15 +26,22 @@ - - + {% if user.is_authenticated %} + + + Sign out + + {% else %} + Sign in + {% endif %} diff --git a/polls/templates/registration/login.html b/polls/templates/registration/login.html new file mode 100644 index 0000000..99361b9 --- /dev/null +++ b/polls/templates/registration/login.html @@ -0,0 +1,37 @@ + + + + + + Sign In Page + + + +
+

Sign In

+
+ {% csrf_token %} +
+

Username

+ {{ form.username }} +
+
+

Password

+ {{ form.password }} +
+ +
+

+ Don't have an account? Sign up here +

+

+ Forget the Password? Reset here +

+ Back to Poll +
+ + diff --git a/polls/templates/registration/password_reset_complete.html b/polls/templates/registration/password_reset_complete.html new file mode 100644 index 0000000..066a37a --- /dev/null +++ b/polls/templates/registration/password_reset_complete.html @@ -0,0 +1,2 @@ +

Password reset complete

+

Your new password has been set. You can log in now on the log in page.

\ No newline at end of file diff --git a/polls/templates/registration/password_reset_confirm.html b/polls/templates/registration/password_reset_confirm.html new file mode 100644 index 0000000..13db400 --- /dev/null +++ b/polls/templates/registration/password_reset_confirm.html @@ -0,0 +1,12 @@ +{% if validlink %} + +

Set a new password!

+
+ {% csrf_token %} + {{ form.as_p }} + +
+ +{% else %} + +

The password reset link was invalid, possibly because it has already been used. Please request a new password reset.

\ No newline at end of file diff --git a/polls/templates/registration/password_reset_done.html b/polls/templates/registration/password_reset_done.html new file mode 100644 index 0000000..fa1ed5a --- /dev/null +++ b/polls/templates/registration/password_reset_done.html @@ -0,0 +1,2 @@ +

Check your inbox.

+

We've emailed you instructions for setting your password. You should receive the email shortly!

\ No newline at end of file diff --git a/polls/templates/registration/password_reset_form.html b/polls/templates/registration/password_reset_form.html new file mode 100644 index 0000000..98b091c --- /dev/null +++ b/polls/templates/registration/password_reset_form.html @@ -0,0 +1,8 @@ +

Forgot your password?

+

Enter your email address below, and we'll email instructions for setting a new one.

+ +
+ {% csrf_token %} + {{ form.as_p }} + +
\ No newline at end of file diff --git a/polls/templates/registration/signup.html b/polls/templates/registration/signup.html new file mode 100644 index 0000000..0bac13d --- /dev/null +++ b/polls/templates/registration/signup.html @@ -0,0 +1,46 @@ + + + + + Sign Up Page + + + +
+
+

Sign Up

+
+ {% csrf_token %} +
+

Username

+ {{ form.username }} +
+
+

Password

+ {{ form.password1 }} +
+

Password Confirmation

+ {{ form.password2 }} +
+ + +

Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.

+

Your password can’t be too similar to your other personal information. , must contain at least 8 characters, can’t be entirely numeric.

+ +
+ {% if form.errors %} + {% for field in form %} + {% if field.errors %} + {% for error in field.errors %} +

{{ error }}

+ {% endfor %} + {% endif %} + {% endfor %} + {% endif %} +
+

Already have an account? Sign in here

+ Back to Poll +
+
+ + \ No newline at end of file diff --git a/polls/urls.py b/polls/urls.py index 45ebddd..7ddb37f 100644 --- a/polls/urls.py +++ b/polls/urls.py @@ -8,4 +8,5 @@ urlpatterns = [ path("/", views.DetailView.as_view(), name="detail"), path("/results/", views.ResultsView.as_view(), name="results"), path("/vote/", views.vote, name="vote"), + path("signup/", views.SignUpView.as_view(), name="signup"), ] diff --git a/polls/views.py b/polls/views.py index 9abf5a9..7c65469 100644 --- a/polls/views.py +++ b/polls/views.py @@ -3,8 +3,9 @@ from django.shortcuts import get_object_or_404, render from django.urls import reverse from django.views import generic from django.utils import timezone -from django.views.generic import TemplateView +from django.urls import reverse_lazy +from .forms import SignUpForm from .models import Choice, Question @@ -66,6 +67,12 @@ class ResultsView(generic.DetailView): return render(self.request, self.template_name, context) +class SignUpView(generic.CreateView): + form_class = SignUpForm + success_url = reverse_lazy('login') + template_name = 'registration/signup.html' + + def vote(request, question_id): """ A function that update the database. Add vote count to choice that user vote