Files
music-storage/music_storage/music/views.py

55 lines
1.5 KiB
Python

from django import views as django_views
from django.views.generic import ListView
from django.shortcuts import render
from django.http.request import HttpRequest
from django.shortcuts import get_object_or_404
from music.models import Track
from music.models import Artist
from music.models import Album
class TrackListView(ListView):
model = Track
template_name = "music/track_list.html"
context_object_name = "tracks"
paginate_by = 25
def get_queryset(self):
return (
super().get_queryset()
.select_related("album", "album__artist")
.order_by("id")
)
class ArtistListView(ListView):
model = Artist
template_name = "music/artist_list.html"
context_object_name = "artists"
paginate_by = 25
def get_queryset(self):
return super().get_queryset().order_by("id")
class ArtistDetailView(django_views.View):
def get(self, request: HttpRequest, pk: int, *args, **kwargs):
artist = get_object_or_404(Artist, id=pk)
return render(request, "music/artist_detail.html", {"artist": artist})
class AlbumListView(ListView):
model = Album
template_name = "music/album_list.html"
context_object_name = "albums"
paginate_by = 25
def get_queryset(self):
return super().get_queryset().order_by("id")
class AlbumDetailView(django_views.View):
def get(self, request: HttpRequest, pk: int, *args, **kwargs):
album = get_object_or_404(Album, id=pk)
return render(request, "music/album_detail.html", {"album": album})