2017-07-19 139 views
-1

我VIEW.PY文件是:視圖music.views.detail沒有返回HttpResponse對象。它返回無不是

from django.http import Http404 
from django.shortcuts import render 
from .models import Album 

def index(request): 
all_albums = Album.objects.all() 
return render(request, 'music/index.html', {'all_albums': all_albums}) 
def detail(request, album_id): 
try: 
    album = Album.objects.get(pk=album_id) 
except Album.DoesNotExist: 
    raise Http404("Album does not exist") 
    return render(request, 'music/detail.html', {'album': album})' 

DETAIL.HTML文件是:

{{ album }} 

我得到錯誤:

The view music.views.detail didn't return an HttpResponse object. It returned None instead

+1

請正確縮進的代碼,以便意思很明顯 – kdopen

+0

縮進我的代碼需要和錯誤被刪除 –

+0

無正確的縮進,這個問題可以關閉爲「不清楚」或「無MCVE」。 – halfer

回答

1

Unident return語句等等它位於except區塊之外。

try: 
    album = Album.objects.get(pk=album_id) 
except Album.DoesNotExist: 
    raise Http404("Album does not exist") 
return render(request, 'music/index.html', {'album': album}) 

請注意,您可以通過使用get_object_or_404快捷略微簡化您的觀點:

from django.shortcuts import render, get_object_or_404 

def detail(request, album_id): 
    album = get_object_or_404(Album, pk=album_id) 
    return render(request, 'music/index.html', {'album': album})' 
相關問題