Add view to go to random video from main page

This commit is contained in:
Kumi 2022-05-08 15:37:29 +02:00
parent 689da931dd
commit 3e52f8a8e3
Signed by: kumi
GPG key ID: 5D1CE6AF1805ECA2
3 changed files with 14 additions and 2 deletions

View file

@ -1,10 +1,11 @@
from django.urls import path
from .views import PlaylistView, VideoView
from .views import PlaylistView, VideoView, RandomView
urlpatterns = [
path('', PlaylistView.as_view(), name="home"),
path('video/<slug:pk>/', VideoView.as_view(), name="video"),
path('playlist/<slug:pk>/', PlaylistView.as_view(), name="playlist"),
path('random/', RandomView.as_view(), name="randomvideo"),
]

View file

@ -1,6 +1,8 @@
from django.views.generic import ListView, DetailView
from django.views.generic import ListView, DetailView, RedirectView
from django.shortcuts import get_object_or_404
from random import choice
from backend.models import Video, Playlist
@ -49,3 +51,10 @@ class VideoView(DetailView):
def get_playlist(self):
if (pid := self.request.GET.get("playlist")):
return get_object_or_404(Playlist, id=pid)
class RandomView(RedirectView):
permanent = False
def get_redirect_url(self, *args, **kwargs):
ids = Video.objects.values_list("id", flat=True)
return Video.objects.get(id=choice(ids)).get_absolute_url()

View file

@ -8,6 +8,8 @@
<div class="row">
<div class="col-md-3">
<ul class="list-unstyled sidebar">
<a href="{% url "randomvideo" %}">Random Video</a>
<hr>
<li><a class="active" href="/">All</a></li>
{% for pl in playlists %}
<li><a href="{{pl.get_absolute_url}}">{{pl.title}}</a></li>