Динамическая маршрутизация в Django 3
Могу переходить страницы по id - /Selected_Podcast/1/2..на которых отображаются отсортированные через ForeignKey полю данные. Как на странице Podcast.html создать циклом карточки со ссылками на страницы /Selected_Podcast/1/2..
urls.py
urlpatterns = [
path('', views.home_page, name='Home'),
path('Beer/', views.beer_page, name='Beer'),
path('Podcast/', views.podcast_page, name='Podcast'),
path('Selected_Podcast/<str:pk>/', views.s_podcast_page, name='Selected_Podcast'),
]
models.py
class Show(models.Model):
PodcastShow = models.CharField(max_length=200, null=True, blank=True)
def __str__(self):
return self.PodcastShow
class Podcast(models.Model):
PodcastShow = models.ForeignKey(Show, on_delete=models.PROTECT)
PodcastShowNumber = models.CharField(max_length=100, null=True, blank=True)
PodcastName = models.TextField(max_length=1000, null=True)
PodcastDescription = models.TextField(max_length=1000, null=True)
PodcastFile = models.FileField(upload_to='images/', null=True, blank=True)
PodcastUrl = models.CharField(max_length=200, null=True, blank=True)
def __str__(self):
return self.PodcastName
view.py
def podcast_page(request):
show = Show.objects.all()
podcast = Podcast.objects.all().order_by('PodcastShow','PodcastShowNumber')
context = {'podcast': podcast, 'show': show,}
return render(request, 'applications/Podcast.html', context)
def s_podcast_page(request, pk):
show = Show.objects.get(id=pk)
option = show.podcast_set.all().order_by('PodcastShowNumber')
context = {'option': option , }
return render(request,'applications/Selected_Podcast.html', context )
Эта страница доступна по id например /Selected_Podcast/1
Selected_Podcast.html
<div class="container">
{% for i in option %}
<div class="row row-cols-3" style="padding: 5px;">
<div class="col" align="left">
<div class="card card-body"> {{i.PodcastName}}</div></div>
<div class="col" align="left">{{i.PodcastDescription}}<br><br></div>
<div class="col" align="left"><div class="card card-body"><audio controls>
<source src="{{i.PodcastFile.url}}"></audio><br><br>
<a class = 'btn btn-sm btn-danger' href="{{i.Podcast}}" target="_blank">podcast website</a></div>
</div>
</div>
{% endfor %}
</div>
Как на этой странице вывести ссылки на Selected_Podcast/1/2...и т.д.
Podcast.html
<div class="container>" align="center">
{% for i in show %}
<div class="row row-cols-2" align="">
<div class="col" align="" style="padding: 50px;">
<div class="card" style="width: 20rem;">
<img src="..." class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title">{{ i.PodcastShow }}</h5>
<p class="card-text">Some quick example text to build on the card
title and make up the bulk of the card's content.</p>
<a href="здесь могла бы быть ссылка" class="btn btn-primary">Go somewhere</a>
</div>
</div>
</div>
{% endfor %}
</div>
</div>