Getting ready

For this and further recipes, we need to extend the music app and add list and detail views there:

  1. Create the views.py file with the following content:
# myproject/apps/music/views.py
from django.views.generic import ListView, DetailView
from django.utils.translation import ugettext_lazy as _
from .models import Song

class SongList(ListView):
model = Song

class SongDetail(DetailView):
model = Song
  1. Create the urls.py file with the following content:
# myproject/apps/music/urls.py
from
django.urls import path
from .views import SongList, SongDetail

app_name = "music"

urlpatterns = [
path("", SongList.as_view(), name="song_list"),
path("<uuid:pk>/", SongDetail.as_view(), name="song_detail"),
]
  1. Include that URL configuration into the project's URL configuration:
# myproject/urls.py
from
django.conf.urls.i18n import i18n_patterns
from django.urls import include, path

urlpatterns = i18n_patterns(
# …
path("songs/", include("myproject.apps.music.urls",
namespace="music")),

)
  1. Create a template for the song list view:
{# music/song_list.html #}
{% extends "base.html" %}
{% load i18n %}

{% block main %}
<ul>
{% for song in object_list %}
<li><a href="{{ song.get_url_path }}">
{{ song }}</a></li>
{% endfor %}
</ul>
{% endblock %}
  1. Then, create one for the song detail view:
{# music/song_detail.html #}
{% extends "base.html" %}
{% load i18n %}

{% block content %}
{% with song=object %}
<h1>{{ song }}</h1>
{% if song.image %}
<img src="{{ song.image.url }}" alt="{{ song }}" />
{% endif %}
{% if song.url %}
<a href="{{ song.url }}" target="_blank"
rel="noreferrer noopener">
{% trans "Check this song" %}
</a>
{% endif %}
{% endwith %}
{% endblock %}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.244.216