Skip to content

Commit

Permalink
User registration
Browse files Browse the repository at this point in the history
  • Loading branch information
yahyaaly151989 committed Sep 21, 2023
1 parent 736a110 commit 15e3bbf
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 3 deletions.
Binary file modified Module04/bookmarks/account/__pycache__/forms.cpython-311.pyc
Binary file not shown.
Binary file modified Module04/bookmarks/account/__pycache__/urls.cpython-311.pyc
Binary file not shown.
Binary file modified Module04/bookmarks/account/__pycache__/views.cpython-311.pyc
Binary file not shown.
17 changes: 17 additions & 0 deletions Module04/bookmarks/account/forms.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
from django import forms
from django.contrib.auth.models import User


class LoginForm(forms.Form):
username = forms.CharField()
password = forms.CharField(widget=forms.PasswordInput)


class UserRegistrationForm(forms.ModelForm):
password = forms.CharField(label= 'password', widget=forms.PasswordInput)
password2 = forms.CharField(label= 'Repeat password', widget=forms.PasswordInput)

class Meta:
model = User
fields = ['username', 'first_name', 'email']

def clean_password2(self):
cd = self.cleaned_data

if cd['password'] != cd['password2']:
raise forms.ValidationError("Passwords don't match.")
return cd['password2']
14 changes: 14 additions & 0 deletions Module04/bookmarks/account/templates/account/register.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{% extends "base.html" %}

{% block title %} Create an account {% endblock title %}

{% block content %}
<h1>Create a new account</h1>
<p>Please, sign up using the following form:</p>

<form method="post">
{{user_form.as_p}}
{% csrf_token %}
<p><input type="submit" value="Create an account"></p>
</form>
{% endblock content%}
11 changes: 11 additions & 0 deletions Module04/bookmarks/account/templates/account/register_done.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% extends "base.html" %}

{% block title %} Welcome {% endblock title %}

{% block content %}
<h1>Welcome {{ new_user.first_name }}</h1>
<p>
Your account has been created successfully.
Now you can <a href="{% url 'login' %}">login</a>
</p>
{% endblock content%}
5 changes: 4 additions & 1 deletion Module04/bookmarks/account/templates/registration/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ <h1>Login</h1>
</p>

{% else %}
<p>Please enter your username and password.</p>
<p>
Please enter your username and password.
If you do not have an account, <a href="{% url 'register' %}">Register here</a>
</p>
{% endif %}

<div class="login-form">
Expand Down
1 change: 1 addition & 0 deletions Module04/bookmarks/account/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

path('', include('django.contrib.auth.urls')),
path('dashboard/', views.dashboard, name='dashboard'),
path('register/', views.register, name='register'),



Expand Down
16 changes: 14 additions & 2 deletions Module04/bookmarks/account/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.shortcuts import render
# from django.http import HttpResponse
# from .forms import LoginForm
from .forms import LoginForm, UserRegistrationForm
# from django.contrib.auth import authenticate, login
from django.contrib.auth.decorators import login_required

Expand All @@ -24,4 +24,16 @@

@login_required
def dashboard(request):
return render(request, 'account/dashboard.html', {'section': 'dashboard'})
return render(request, 'account/dashboard.html', {'section': 'dashboard'})

def register(request):
if request.method == 'POST':
user_form = UserRegistrationForm(request.POST)
if user_form.is_valid():
new_user = user_form.save(commit=False)
new_user.name = new_user.set_password(user_form.cleaned_data['password'])
new_user.save()
return render(request, 'account/register_done.html', {'new_user': new_user})
else:
user_form = UserRegistrationForm()
return render(request, 'account/register.html', {'user_form': user_form})
Binary file modified Module04/bookmarks/db.sqlite3
Binary file not shown.

0 comments on commit 15e3bbf

Please sign in to comment.