Skip to content

Commit ad19949

Browse files
committed
Add basic register, login, logout functionality
1 parent b712945 commit ad19949

24 files changed

+276
-17
lines changed

accounts/__init__.py

Whitespace-only changes.

accounts/admin.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.contrib import admin
2+
3+
# Register your models here.

accounts/apps.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.apps import AppConfig
2+
3+
4+
class AccountsConfig(AppConfig):
5+
name = 'accounts'

accounts/forms.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from django import forms
2+
from django.contrib.auth.models import User
3+
from django.contrib.auth.forms import UserCreationForm
4+
from django.core.exceptions import ValidationError
5+
6+
7+
class UserLoginForm(forms.Form):
8+
"""Form to log users in"""
9+
username = forms.CharField()
10+
password = forms.CharField(widget=forms.PasswordInput)
11+
12+
13+
class UserRegistrationForm(UserCreationForm):
14+
"""Form to register new user"""
15+
password1 = forms.CharField(
16+
label="Password", widget=forms.PasswordInput)
17+
password2 = forms.CharField(
18+
label="Confirm Password",
19+
widget=forms.PasswordInput)
20+
21+
class Meta:
22+
model = User
23+
fields = ['email', 'username', 'password1', 'password2']
24+
25+
def clean_email(self):
26+
email = self.cleaned_data.get('email')
27+
username = self.cleaned_data.get('username')
28+
if User.objects.filter(email=email).exclude(username=username):
29+
raise forms.ValidationError(u'Email address already exists')
30+
return email
31+
32+
def clean_password2(self):
33+
password1 = self.cleaned_data.get('password1')
34+
password2 = self.cleaned_data.get('password2')
35+
36+
if not password1 or not password2:
37+
raise ValidationError("Please confirm password.")
38+
39+
if password1 != password2:
40+
raise ValidationError("Passwords must match.")
41+
42+
return password2

accounts/migrations/__init__.py

Whitespace-only changes.

accounts/models.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.db import models
2+
3+
# Create your models here.

accounts/templates/index.html

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{% extends 'base.html' %}
2+
{% block page_heading %}
3+
<h1>Home</h1>
4+
{% endblock %}
5+
{% block content %}
6+
<br>
7+
<hr>
8+
{% if messages %}
9+
<div>
10+
{% for message in messages %}
11+
{{ message }}
12+
{% endfor %}
13+
</div>
14+
{% endif %}
15+
{% endblock %}

accounts/templates/login.html

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{% extends 'base.html' %}
2+
{% load bootstrap_tags %}
3+
{% block title %}
4+
Login
5+
{% endblock %}
6+
{% block page_heading %}
7+
<h1>Login</h1>
8+
{% endblock %}
9+
{% block content %}
10+
<br>
11+
<form method="POST">
12+
{% csrf_token %}
13+
{{ login_form | as_bootstrap }}
14+
<button type="submit" class="btn btn-primary">Log In</button>
15+
</form>
16+
<hr>
17+
<p>Don't have an account yet? <a href="{% url 'registration' %}">Register</a></p>
18+
{% if messages %}
19+
<div>
20+
{% for message in messages %}
21+
{{ message }}
22+
{% endfor %}
23+
</div>
24+
{% endif %}
25+
{% endblock %}

accounts/templates/profile.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{% extends 'base.html' %}
2+
{% block title %}{{ user }}'s Profile{% endblock %}
3+
{% block page_heading %}
4+
<h1>Profile</h1>
5+
{% endblock %}
6+
{% block content %}
7+
{{ user }}
8+
<p>{{ profile.email }}</p>
9+
{% endblock %}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{% extends 'base.html' %}
2+
{% load bootstrap_tags %}
3+
{% block title %}
4+
Registration
5+
{% endblock %}
6+
{% block content %}
7+
<h1>Registration</h1>
8+
<br>
9+
<form method="POST">
10+
{% csrf_token %}
11+
{{ registration_form | as_bootstrap }}
12+
<button type="submit" class="btn btn-primary">Register</button>
13+
</form>
14+
<hr>
15+
<p>If you already have an account, <a href="{% url 'login' %}">Log In</a></p>
16+
{% endblock %}

0 commit comments

Comments
 (0)