Add part 4f, make it redirect and count

parent e482ee51
......@@ -107,3 +107,6 @@ Edit `urlapp/views.py`.
Higher-level URLs.
Edit `urlapp/templates/index.html`.
Make it redirect and count!
Edit `urlapp/views.py`.
from django.shortcuts import render, get_object_or_404
from django.shortcuts import render, get_object_or_404, redirect
from django.http import HttpResponse
from .models import Url
......@@ -12,7 +12,10 @@ def index(request):
return render(request, 'index.html', context)
def redir(request, url_id):
return HttpResponse("You're following URL {}.".format(url_id))
u = get_object_or_404(Url, id=url_id)
u.used += 1
u.save()
return redirect(u.target)
def url(request, url_id):
context = {
......
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