Add part 5, user authentication

parent e9a5d5b2
...@@ -110,3 +110,10 @@ Edit `urlapp/templates/index.html`. ...@@ -110,3 +110,10 @@ Edit `urlapp/templates/index.html`.
Make it redirect and count! Make it redirect and count!
Edit `urlapp/views.py`. Edit `urlapp/views.py`.
Add a base template, using bootstrap!
Part 5
------
Require user authentication.
...@@ -101,3 +101,8 @@ USE_TZ = True ...@@ -101,3 +101,8 @@ USE_TZ = True
# https://docs.djangoproject.com/en/1.8/howto/static-files/ # https://docs.djangoproject.com/en/1.8/howto/static-files/
STATIC_URL = '/static/' STATIC_URL = '/static/'
# Authentication
LOGIN_URL = '/login/'
...@@ -15,8 +15,10 @@ Including another URLconf ...@@ -15,8 +15,10 @@ Including another URLconf
""" """
from django.conf.urls import include, url from django.conf.urls import include, url
from django.contrib import admin from django.contrib import admin
from django.contrib.auth import views as auth_views
urlpatterns = [ urlpatterns = [
url(r'^', include('urlapp.urls')), url(r'^', include('urlapp.urls')),
url(r'^', include('django.contrib.auth.urls')),
url(r'^admin/', include(admin.site.urls)), url(r'^admin/', include(admin.site.urls)),
] ]
...@@ -16,8 +16,13 @@ ...@@ -16,8 +16,13 @@
URL follower URL follower
</a> </a>
</div> </div>
<ul class="nav navbar-nav navbar-right">
{% if user.is_authenticated %}
<li><a href="{% url 'logout' %}">Logout</a></li>
{% endif %}
</ul>
</div> </div>
</nav> </nav>
<div class="container"> <div class="container">
<div class="row"> <div class="row">
{% block content %} {% block content %}
......
{% 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.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 from .models import Url
# Create your views here. # Create your views here.
@login_required
def index(request): def index(request):
context = { context = {
'url_list': Url.objects.all() 'url_list': Url.objects.all()
...@@ -17,6 +18,7 @@ def redir(request, url_id): ...@@ -17,6 +18,7 @@ def redir(request, url_id):
u.save() u.save()
return redirect(u.target) return redirect(u.target)
@login_required
def url(request, url_id): def url(request, url_id):
context = { context = {
'url': get_object_or_404(Url, id=url_id) '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