Add support for new URLs

parent 153b80c1
......@@ -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.details, name='details'),
url(r'^new$', views.new, name='new'),
url(r'^generate$', views.generate, name='generate'),
]
from django.shortcuts import render, redirect, get_object_or_404
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from .models import Url
# Create your views here.
......@@ -24,3 +27,14 @@ def details(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):
u = Url(target=request.POST['target'], user=request.user)
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