如何从其他详细信息视图中查看详细信息并正确获取模板中的所有QuerySet



我需要从其他详细信息查看详细信息。但是我无法清楚地传递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) ??

清楚地获得两个模板的所有QuerySet

我仍然不太了解您在哪里有问题。

如果您只想将两个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)

相关内容

最新更新