Add part 5, user authentication

parent e9a5d5b2
......@@ -110,3 +110,10 @@ Edit `urlapp/templates/index.html`.
Make it redirect and count!
Edit `urlapp/views.py`.
Add a base template, using bootstrap!
Part 5
------
Require user authentication.
......@@ -101,3 +101,8 @@ USE_TZ = True
# https://docs.djangoproject.com/en/1.8/howto/static-files/
STATIC_URL = '/static/'
# Authentication
LOGIN_URL = '/login/'
......@@ -15,8 +15,10 @@ Including another URLconf
"""
from django.conf.urls import include, url
from django.contrib import admin
from django.contrib.auth import views as auth_views
urlpatterns = [
url(r'^', include('urlapp.urls')),
url(r'^', include('django.contrib.auth.urls')),
url(r'^admin/', include(admin.site.urls)),
]
......@@ -16,6 +16,11 @@
URL follower
</a>
</div>
<ul class="nav navbar-nav navbar-right">
{% if user.is_authenticated %}
<li><a href="{% url 'logout' %}">Logout</a></li>
{% endif %}
</ul>
</div>
</nav>
<div class="container">
......
{% extends "base.html" %}
{% block content %}
{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}
{% if next %}
{% if user.is_authenticated %}
<p>Your account doesn't have access to this page. To proceed,
please login with an account that has access.</p>
{% else %}
<p>Please login to see this page.</p>
{% endif %}
{% endif %}
<form method="post" action="{% url 'django.contrib.auth.views.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="{{ next }}" />
</form>
{# Assumes you setup the password_reset view in your URLconf #}
<p><a href="{% url 'password_reset' %}">Lost password?</a></p>
{% endblock %}
from django.shortcuts import render, get_object_or_404, redirect
from django.http import HttpResponse
from django.contrib.auth.decorators import login_required
from .models import Url
# Create your views here.
@login_required
def index(request):
context = {
'url_list': Url.objects.all()
......@@ -17,6 +18,7 @@ def redir(request, url_id):
u.save()
return redirect(u.target)
@login_required
def url(request, url_id):
context = {
'url': get_object_or_404(Url, id=url_id)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment