Skip to content

Commit

Permalink
we implemented authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
cookjw committed Aug 31, 2014
1 parent 14a6dc8 commit a7309c6
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 3 deletions.
8 changes: 7 additions & 1 deletion core/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from django.shortcuts import render
from django.shortcuts import render, redirect
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_POST
from django.contrib.auth import logout

from core.models import Post, Comment

Expand Down Expand Up @@ -35,3 +36,8 @@ def ballot_box(request, kind, pk):
return HttpResponse(status=204)
else:
return HttpResponse(status=400)

def logout_view(request):
logout(request)
return redirect("/")

2 changes: 2 additions & 0 deletions settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@

AUTH_USER_MODEL = "core.FinetoothUser"

AUTH_REDIRECT_URL = "/"

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'
Expand Down
8 changes: 6 additions & 2 deletions templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@
<span id="brand" class="pull-left"><a href="/">Finetooth</a></span>
<ul class="nav navbar-nav">
<li class="active"><a href="#">Home</a></li>
<li><a href="#TODO">About</a></li>
<li><a href="#TODO">Log in</a></li>
{% if not user.is_authenticated %}
<li><a href="/login">Log in</a></li>
<li><a href="#TODO">Sign up</a></li>
{% else %}
<li>{{ user.username }}</li>
<li> <a href="/logout">Log out</a></li>
{% endif %}
</ul>
</div>
</div>
Expand Down
28 changes: 28 additions & 0 deletions templates/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{% extends "base.html" %}

{% block subtitle %}Login{% endblock %}

{% block content %}
{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
<p>{{ form.errors }}</p>
{% endif %}

<form method="post" action="{% url 'login' %}">
{% csrf_token %}
<table>
<tr>
<td>{{ form.username.label_tag }}</td>
<td>{{ form.username }}</td>
</tr>
<tr>
<td>{{ form.password.label_tag }}</td>
<td>{{ form.password }}</td>
</tr>
</table>

<input type="submit" value="login" />
<input type="hidden" name="next" value="/"/>
</form>

{% endblock %}
3 changes: 3 additions & 0 deletions urls.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from django.conf.urls import patterns, include, url
from django.contrib.auth.views import login

urlpatterns = patterns(
'',
url(r'^$', 'core.views.home', name='home'),
url(r'^(\d+)/$', 'core.views.show_post', name="show_post"),
url(r'^vote/(?P<kind>.+)/(?P<pk>\d+)/$',
'core.views.ballot_box', name="vote"),
url(r'^login/', login, {'template_name': "login.html"}, name="login"),
url(r'^logout/$', 'core.views.logout_view', name='logout'),
)

0 comments on commit a7309c6

Please sign in to comment.