Sunday 3 March 2019

django 8 template



#music/views

from django.shortcuts import render
from django.http import  HttpResponse
from .models import Album
from django.template import loader

def index(request):
    all_albums = Album.objects.all()
    template = loader.get_template('music/index.html')
    context = {
        'all_albums': all_albums,
    }

    return HttpResponse(template.render(context, request))

def detail(request, album_id):
    return HttpResponse('<h2>Details for Album id: ' + str(album_id) + '</h2>')

------------------------------
#music/templates/music/index

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Albums</title>
</head>
<body>

    {% if all_albums %}
        <ul>
            {% for album in all_albums %}
                <li><a href="/music/{{album.id}}">{{ album.album_title }}</a></li>
            {% endfor %}
        </ul>
    {% else %}
        <h3>There is no album</h3>
    {% endif %}
</body>
</html>

-----------------------
def index(request):
    all_albums = Album.objects.all()
    html=''
    #url = '/music/' + str(album.id) + '/'
        #html += '<a href=" '+ url +' ">' + album.album_title + '</a><br>'
    return HttpResponse(html)

No comments:

Post a Comment