Add part 4d, add 404 for bad queries

parent 2b369b88
...@@ -101,3 +101,7 @@ Make views that do something, just reading the objects' data. ...@@ -101,3 +101,7 @@ Make views that do something, just reading the objects' data.
Make views using templates. Make views using templates.
Edit `urlapp/views.py`. Edit `urlapp/views.py`.
Create `urlapp/templates/index.html` and `urlapp/templates/details.html`. Create `urlapp/templates/index.html` and `urlapp/templates/details.html`.
Add 404.
Edit `urlapp/views.py`.
from django.shortcuts import render from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse from django.http import HttpResponse
from .models import Url from .models import Url
...@@ -16,6 +16,6 @@ def redir(request, url_id): ...@@ -16,6 +16,6 @@ def redir(request, url_id):
def url(request, url_id): def url(request, url_id):
context = { context = {
'url': Url.objects.get(id=url_id) 'url': get_object_or_404(Url, id=url_id)
} }
return render(request, 'details.html', context) return render(request, 'details.html', 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