Add support for new URLs

parent fd473073
......@@ -117,3 +117,8 @@ Part 5
------
Require user authentication.
Part 6
------
Add new URLs.
......@@ -18,6 +18,7 @@
</div>
<ul class="nav navbar-nav navbar-right">
{% if user.is_authenticated %}
<li><a href="{% url 'new' %}">New</a></li>
<li><a href="{% url 'logout' %}">Logout</a></li>
{% endif %}
</ul>
......
{% extends "base.html" %}
{% block content %}
<h1>Add new URL</h1>
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<form action="{% url 'generate' %}" method="post">
{% csrf_token %}
<label for="target">Target:</label><br />
<input type="text" name="target" id="target" />
<input type="submit" value="Generate" />
</form>
{% endblock %}
......@@ -6,4 +6,6 @@ urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^(?P<url_id>[0-9]+)/$', views.redir, name='redir'),
url(r'^url=(?P<url_id>[0-9]+)/$', views.url, name='url'),
url(r'^new/$', views.new, name='new'),
url(r'^generate/$', views.generate, name='generate'),
]
from django.shortcuts import render, get_object_or_404, redirect
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseRedirect, HttpResponse
from django.core.urlresolvers import reverse
from .models import Url
# Create your views here.
......@@ -24,3 +27,23 @@ def url(request, url_id):
'url': get_object_or_404(Url, id=url_id)
}
return render(request, 'details.html', context)
@login_required
def new(request):
context = {
}
return render(request, 'new.html', context)
@login_required
def generate(request):
try:
u = Url(target=request.POST['target'])
request.user.url_set.add(u)
except:
# Redisplay the question voting form.
return render(request, 'new.html', {
'error_message': "Something went wrong.",
})
else:
u.save()
return HttpResponseRedirect(reverse('index'))
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