raise 404 错误:
def detail(request, album_id):
try:
album = Album.objects.filter(pk=album_id)
except Album.DoesNotExist:
raise Http404("Album does not exist")
context = {
"album":album,
}
return render(request, "music/detail.html", context)
里面的:
try:
album = Album.objects.filter(pk=album_id)
except Album.DoesNotExist:
raise Http404("Album does not exist")
等价于:
album = get_object_or_404(Album, pk=album_id)