简体   繁体   中英

Django: Use 2nd argument given by url

I want to make a Django website where I can upload series with seasons and episodes. In the end it should look something like this:

Seasons:

[1] [2] [3]

Episodes:

[1] [2] [3] [4] [5] [6]  

The Episodes belong to the season given by the url.

My code looks like this:

models.py:

class Serie(models.Model):
    title = models.CharField(max_length=120)
    thumbnail = models.ImageField(upload_to=serie_dir_path)
    ...


class Season(models.Model):
    order = models.IntegerField()
    serie = models.ForeignKey(Serie, on_delete=models.CASCADE)
    ...

class Episode(models.Model):
    title = models.CharField(max_length=120)
    order = models.IntegerField()
    episode = models.FileField(upload_to=episode_dir_path)
    season = models.ForeignKey(Season, on_delete=models.CASCADE)
    ...

views.py:

class SeriesDetailView(DetailView):
    model = Serie

    def get_context_data(self, **kwargs):
        context = super(SeriesDetailView, self).get_context_data(**kwargs)
        context.update({
            'seasons': Season.objects.all(),
            'episodes': Episode.objects.all()
        })
        return context

urls.py:

urlpatterns = [
    path('library', LibListView.as_view(), name='library'),
    path('series/<int:pk>/season/<int:order>', SeriesDetailView.as_view(), name='serie-detail'),
]

lib.html:

<a href="{% url 'serie-detail' serie.id 1 %}">

(The 1 is because I want the first season)

serie_detail.html:

<section class="section mt-5">
        <div class="container">
        <h1>{{ serie.title }}</h1>
        <img class="card-img-top mb-2" src="{{ serie.thumbnail.url }}" style="height: 319px; width: 220px;">
        <h6>Uploaded by: <h6 class="category text-warning"> {{serie.createdUser.username}}</h6></h6>
            <div class="row">
                <h4 class="mb-0">Seasons:</h4>
            </div>
            <div class="row">
            {% for season in serie.season_set.all %}
                <a href="" class="btn btn-default float-left">{{ season.order }}</a>
            {% endfor %}
            </div>
            <div class="row">
                <h4 class="mb-0">Episodes:</h3>
            </div>
            <div class="row">
            {% for season in serie.season_set.all %}
                {% for episode in season.episode_set.all %}
                    <a href="{% url 'episode-detail' episode.id %}" class="btn btn-default float-left btn-sm">{{ episode.order }}</a>
                {% endfor %}
            {% endfor %}
            </div>
        </div>
    </section>

With the code above the result looks like this:

Seasons:

[1] [2]  

Episodes:

[1] [1] 

It takes the first episode of the 2nd season and also shows that.

I'm grateful for any help.

I assume you need to display all the seasons episodes of only one particular season (season 1 in this case). You would need to modify the code something like below:

class SeriesDetailView(DetailView):
    model = Serie

    def get_context_data(self, **kwargs):
        order = kwargs.get("order")
        context = super(SeriesDetailView, self).get_context_data(**kwargs)
        context.update({
            'seasons': Season.objects.all(),
            'episodes': Episode.objects.filter(season=order)
        })
        return context

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM