Wednesday 13 March 2019

django 16 generic views



#music/urls

from django.urls import path
from . import views

app_name = 'music'

urlpatterns = [
    path('', views.IndexView.as_view(), name='index'),

    #/music/id/
    path('<pk>/', views.DetailView.as_view(), name='detail'),

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

from django.views import generic
from .models import Album

class IndexView(generic.ListView):
    template_name = 'music/index.html'

    def get_queryset(self):
        return  Album.objects.all()

class DetailView(generic.DeleteView):
    model = Album
    template_name = 'music/detail.html'

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

{% extends 'music/base.html' %}
{% block title %}Albums{% endblock %}

{% block body %}

    <ul>
        {% for album in object_list %}
            <li><a href="{% url 'music:detail' album.id %}">{{ album.album_title }}</a></li>
        {% endfor %}
    </ul>

{% endblock %}

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

{% extends 'music/base.html' %}
{% block title %}Songs{% endblock %}

{% block body %}
    <img style="height: 150px;" src="{{album.album_logo}}">
    <h1>{{album.album_title}}</h1>
    <h3>{{album.artist}}</h3>

    {% for song in album.song_set.all %}
        {{ song.song_title }}

        {% if song.is_favorite %}
            <img src="http://i.imgur.com/b9b13Rd.png"><br>
        {% endif %}
        <br>
    {% endfor %}

{% endblock %}

No comments:

Post a Comment