Django несколько шаблонов в одном представлении - PullRequest
0 голосов
/ 11 июля 2019

У меня есть 2 шаблона: сначала одна команда против команды и последний игрок против игрока.Например, Dota2 соответствует команде против команды.Тем не менее, сердечные спички игрок против игрока.мой путь URL '<str:gameslug>/turnuva/<str:tournamentslug>/mac/<str:lolslug>'

Я хочу вот так:

if gameslug==lol return render template1 
if gameslug==heartstone return template2

Как мне это сделать?

    def game_detail(request,tournamentslug,lolslug,gameslug):

        game = get_object_or_404(
            LeagueofLegendsGame,
            tournament__tournament_slug=tournamentslug,
            lol_slug=lolslug,
            tournament__game__slug=gameslug
        )

        context={
            'game':game,

        }

        return render(request,'esports/lolgame.html',context)

1 Ответ

2 голосов
/ 11 июля 2019

Ну, вы можете проверить, что находится в переменной gameslug перед рендерингом.

def game_detail(request,tournamentslug,lolslug,gameslug):

    game = get_object_or_404(
        LeagueofLegendsGame,
        tournament__tournament_slug=tournamentslug,
        lol_slug=lolslug,
        tournament__game__slug=gameslug
    )

    context={
        'game':game,
    }

    if gameslug == 'lol':
        template = 'template1.html'
    elif gameslug == 'heartstone':
        template = 'template2.html'
    #else render the one you're already rendering
    else:
        template = 'esports/lolgame.html'

    return render(request, template, context)
...