user3667832
user3667832

Reputation: 377

Django - can't add new value to view context

I have dictionary that i pass as context in my django view, it contains contents of one of the database objects.

I want to add new value to dictionary, to pass additional variable to template.

This code unfortunetly wont work. "new" is not added to "k" and passed to template:

def view(request):

    lista_baza = Usluga.objects.all()
    k= {
        'lista_uslug': lista_baza
    }


    k.update({'new': 'newcontent'})

    return render(request, 'main.html', k)

Upvotes: 0

Views: 406

Answers (2)

zabusa
zabusa

Reputation: 2719

def view(request):  
    lista_baza = Usluga.objects.all()
    k= {
        'lista_uslug': lista_baza
    }


    k['new'] = 'your context'

    return render(request, 'main.html', k)

Upvotes: 0

Jingo
Jingo

Reputation: 3240

You could add a new key/value pair to your dictionary like this:

k['new']='newcontent'

Hope this helps.

Upvotes: 1

Related Questions