我需要从其他详细信息查看详细信息。但是我无法清楚地传递QuerySet ...
在模型中:
class Match(models.Model):
mega_league = models.ManyToManyField('MegaLeague', blank=True)
class MegaPricePoolBreak(models.Model):
pass
class MegaLeague(models.Model):
price_pool_break = models.ManyToManyField(MegaPricePoolBreak, blank=True)
在视图中:
def league(request):
match = Match.objects.all()
context = {
'match': match,
}
return render(request, 'main/league.html', context=context)
def league_detail(request, pk):
match = get_object_or_404(Match, pk=pk)
context = {
'match': match,
}
return render(request, 'main/league_detail.html', context=context)
def league_detail_more(request, pk):
match = get_object_or_404(Match, pk=pk)
context = {
'match': match,
'title': 'select matches',
}
return render(request, 'main/league_detail_more.html', context=context)
在league
模板中,我通过{% url 'league_detail' match.pk %}
将QuerySet从Match
获取到league_detail
模板和league_detail
模板中,我通过{% url 'league_detail_more' match.pk %}
----这是主要问题。match.pk和match.mega_league.pk 将querySet从 Match
获取到 league_detail_more
模板..
在所有模板中我都使用 for
循环..它的工作...但是要获得特定的pk查询不起作用。
它适用于league_detail
模板,但对于league_detail_more
模板。league_detail_more
CC_13 template pk pass从league_detail
不起作用。
如何使用match = get_object_or_404(Match, pk=pk)
??
我仍然不太了解您在哪里有问题。
如果您只想将两个ID传递给URL,那很简单:
path('league_detail_more/<int:match_id>/<int:league_id>/', league_detail_more, name='league_detail_more')
和视图:
def league_detail_more(request, match_id, league_id):
match = get_object_or_404(Match, pk=match_id)
league = get_object_or_404(MegaLeague, pk=league_id)