Tuesday, 5 March 2019

django 10 404





#music/views

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

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})

---------------------------------
#music/templates/music/detail

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Detail</title>
</head>
<body>
   {{album}}
</body>
</html>

---------------------------------------------
#music/views

from .models import Album
from django.shortcuts import render, get_object_or_404

def index(request):
    all_albums = Album.objects.all()
    return render(request, 'music/index.html', {'all_albums': all_albums,})

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

No comments:

Post a Comment