Add view to go to random video from main page
This commit is contained in:
parent
689da931dd
commit
3e52f8a8e3
3 changed files with 14 additions and 2 deletions
|
@ -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"),
|
||||
]
|
||||
|
|
|
@ -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()
|
|
@ -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>
|
||||
|
|
Loading…
Reference in a new issue