Enes
Enes

Reputation: 341

Django multiple templates in single view

I have 2 templates first one team vs team and last one player vs player. For example, Dota2 matches team vs team. However, heartstone matches are player vs player. my url path '<str:gameslug>/turnuva/<str:tournamentslug>/mac/<str:lolslug>'

I want like this:

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

How can i do?

    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)

Upvotes: 0

Views: 161

Answers (1)

Higor Rossato
Higor Rossato

Reputation: 2046

Well, you can basically check what's in the gameslug variable before rendering.

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)

Upvotes: 2

Related Questions