Skip to content

Commit b038737

Browse files
committed
A shortcut: get_object_or_404()
modified: mysite/polls/views.py https://docs.djangoproject.com/en/1.8/intro/tutorial03/#a-shortcut-get-object-or-404
1 parent 2c5fbe8 commit b038737

File tree

1 file changed

+2
-6
lines changed

1 file changed

+2
-6
lines changed

mysite/polls/views.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from django.http import HttpResponse
2-
from django.http import Http404
3-
from django.shortcuts import render
2+
from django.shortcuts import get_object_or_404, render
43

54
from .models import Question
65

@@ -11,10 +10,7 @@ def index(request):
1110
return render(request, 'polls/index.html', context)
1211

1312
def detail(request, question_id):
14-
try:
15-
question = Question.objects.get(pk=question_id)
16-
except Question.DoesNotExist:
17-
raise Http404("Question does not exist")
13+
question = get_object_or_404(Question, pk=question_id)
1814
return render(request, 'polls/detail.html', {'question': question})
1915

2016
def results(request, question_id):

0 commit comments

Comments
 (0)