Skip to content

Commit 2404262

Browse files
committed
Add user registration model
1 parent 60433e1 commit 2404262

File tree

11 files changed

+240
-0
lines changed

11 files changed

+240
-0
lines changed

dgentledjango/urls.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
url(r'^forminput/$', 'forminput.views.index'),
1616

1717
## User Registration
18+
url(r'^userreg/$', 'userreg.views.index'),
19+
url(r'^userreg/band/(?P<band_id>\d+)/$', 'userreg.views.banddetail'),
20+
url(r'^userreg/band/$', 'userreg.views.bandindex'),
21+
url(r'^accounts/logout/$', 'userreg.views.logoutuser'),
22+
url(r'^accounts/login/$', 'userreg.views.loginuser'),
1823
url(r'^accounts/', include('registration.backends.default.urls')),
1924

2025
## Administration

intro/index.html

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Dgentle Django</title>
5+
<meta charset="utf-8">
6+
<link href="/static/css/bootstrap.min.css" rel="stylesheet">
7+
</head>
8+
9+
<body>
10+
<div class="container-fluid">
11+
<div class="row-fluid">
12+
<div class="span2">
13+
<div class="well sidebar-nav">
14+
<ul class="nav nav-list">
15+
<li class="nav-header">Teachers</li>
16+
{% if teacher_list %}
17+
{% for teacher in teacher_list %}
18+
<li><a href="/teacher/{{ teacher.id }}/">{{ teacher.name }}</a></li>
19+
{% endfor %}
20+
{% else %}
21+
<li>No teachers in this class!</li>
22+
{% endif %}
23+
<li class="nav-header">Students</li>
24+
{% if student_list %}
25+
{% for student in student_list %}
26+
<li><a href="/student/{{ student.id }}/">{{ student.name }}</a></li>
27+
{% endfor %}
28+
{% else %}
29+
<li>No students in this class!</li>
30+
{% endif %}
31+
</ul>
32+
</div>
33+
</div> <!-- /span -->
34+
<div class="span6">
35+
<div class="hero-unit">
36+
<h1>Dgentle Django</h1>
37+
<p>Welcome to Dgentle Django! Our goal is to give an introduction to Django and get you started on your own websites.</p>
38+
</div>
39+
</div>
40+
</div>
41+
</div>
42+
</body>
43+
</html>

templates/base.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<title>{% block title %}{% endblock %}</title>
5+
</head>
6+
<body>
7+
{% block content %}{% endblock %}
8+
</body>
9+
</html>

templates/userreg/index.html

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Dgentle Django</title>
5+
<meta charset="utf-8">
6+
<link href="/static/css/bootstrap.min.css" rel="stylesheet">
7+
</head>
8+
9+
<body>
10+
<div class="navbar">
11+
<div class="navbar-inner">
12+
<div class="container">
13+
<ul class="nav pull-right">
14+
{% if user %}
15+
<li><a href="#">Welcome Back {{ user }}!</a></li>
16+
<li><a href="/accounts/logout">Logout</a></li>
17+
{% else %}
18+
{% if error %}
19+
<li><span class="label label-important">{{ error }}</span></li>
20+
{% endif %}
21+
<li>
22+
<form id="loginform" class="navbar-form" method="POST" action="/accounts/login/">
23+
{% csrf_token %}
24+
<input class="span2" type="text" name="username" placeholder="username">
25+
<input class="span2" type="password" name="password" placeholder="password">
26+
</form>
27+
</li>
28+
<li><a href="#" onclick="document.forms['loginform'].submit()">Login</a></li>
29+
<li><a href="/accounts/register">Register</a></li>
30+
{% endif %}
31+
</ul>
32+
</div>
33+
</div>
34+
</div>
35+
<div class="container-fluid">
36+
<div class="row-fluid">
37+
<div class="span2">
38+
<div class="well sidebar-nav">
39+
<ul class="nav nav-list">
40+
<li class="nav-header">Teachers</li>
41+
{% if teacher_list %}
42+
{% for teacher in teacher_list %}
43+
<li><a href="/teacher/{{ teacher.id }}/">{{ teacher.name }}</a></li>
44+
{% endfor %}
45+
{% else %}
46+
<li>No teachers in this class!</li>
47+
{% endif %}
48+
<li class="nav-header">Students</li>
49+
{% if student_list %}
50+
{% for student in student_list %}
51+
<li><a href="/student/{{ student.id }}/">{{ student.name }}</a></li>
52+
{% endfor %}
53+
{% else %}
54+
<li>No students in this class!</li>
55+
{% endif %}
56+
<li class="nav-header">Hair Metal Bands</li>
57+
{% if band_list %}
58+
{% for band in band_list %}
59+
<li><a href="/band/{{ band.id }}/">{{ band.name }}</a></li>
60+
{% endfor %}
61+
{% else %}
62+
<li>No bands found!</li>
63+
{% endif %}
64+
</ul>
65+
</div>
66+
</div> <!-- /span -->
67+
<div class="span6">
68+
<div class="hero-unit">
69+
<h1>Dgentle Django</h1>
70+
<p>
71+
Welcome to Dgentle Django! Our goal is to give an
72+
introduction to Django and get you started on your
73+
own websites.
74+
</p>
75+
</div>
76+
<div class="span4">
77+
<h2>Add band</h2>
78+
<form class="well" method="POST" action="/userreg/band/">
79+
{% csrf_token %}
80+
<label>Band Name</label>
81+
<input type="text" class="span10" name="name" placeholder="band name..." />
82+
<button type="submit" class="btn">Submit</button>
83+
</form>
84+
</div>
85+
</div>
86+
</div>
87+
</div>
88+
</body>
89+
</html>

userreg/__init__.py

Whitespace-only changes.

userreg/__init__.pyc

142 Bytes
Binary file not shown.

userreg/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.

userreg/models.pyc

196 Bytes
Binary file not shown.

userreg/tests.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
This file demonstrates writing tests using the unittest module. These will pass
3+
when you run "manage.py test".
4+
5+
Replace this with more appropriate tests for your application.
6+
"""
7+
8+
from django.test import TestCase
9+
10+
11+
class SimpleTest(TestCase):
12+
def test_basic_addition(self):
13+
"""
14+
Tests that 1 + 1 always equals 2.
15+
"""
16+
self.assertEqual(1 + 1, 2)

userreg/views.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Create your views here.
2+
3+
from django.shortcuts import render_to_response
4+
from django.http import HttpResponse, HttpResponseRedirect
5+
from django.template import Context, loader, RequestContext
6+
from django.contrib.auth.decorators import login_required
7+
from django.contrib.auth import logout, authenticate, login
8+
from classlist.models import Teacher, Student
9+
from forminput.models import Band
10+
from forminput.forms import BandForm
11+
12+
def index(request):
13+
teacher_list = None
14+
student_list = None
15+
band_list = None
16+
user = None
17+
error = None
18+
if 'err' in request.GET and request.GET['err'] == 'invalid':
19+
error = 'Invalid username or password'
20+
21+
if request.user.is_authenticated():
22+
teacher_list = Teacher.objects.all()
23+
student_list = Student.objects.all()
24+
band_list = Band.objects.all()
25+
user = request.user
26+
27+
return render_to_response('userreg/index.html',
28+
{'teacher_list': teacher_list,
29+
'student_list': student_list,
30+
'band_list': band_list,
31+
'user': user,
32+
'error': error},
33+
context_instance = RequestContext(request))
34+
35+
def banddetail(request, band_id):
36+
try:
37+
band = Band.objects.get(pk=band_id)
38+
except Band.DoesNotExist:
39+
raise Http404
40+
41+
return render_to_response('userreg/band_detail.html',
42+
{'band': band})
43+
44+
@login_required(login_url='/accounts/login/')
45+
def bandindex(request):
46+
if request.method == "POST":
47+
form = BandForm(request.POST)
48+
try:
49+
if form.is_valid():
50+
form.save()
51+
return HttpResponseRedirect('/userreg/')
52+
else:
53+
print "well..."
54+
print request.POST
55+
except Exception, e:
56+
print e
57+
58+
def logoutuser(request):
59+
print "Logging out user"
60+
if request.user.is_authenticated():
61+
logout(request)
62+
return HttpResponseRedirect('/userreg/')
63+
else:
64+
print "Already logged out"
65+
return HttpResponseRedirect('/userreg/')
66+
67+
def loginuser(request):
68+
username = request.POST['username']
69+
password = request.POST['password']
70+
user = authenticate(username=username, password=password)
71+
if user is not None:
72+
login(request, user)
73+
else:
74+
return HttpResponseRedirect('/userreg/?err=invalid')
75+
return HttpResponseRedirect('/userreg/')

0 commit comments

Comments
 (0)