Django render third argument
Question:
I am confused with Django renderring. I usually used context and dictionary in the third argument:
return render(request,'zing_it/home.html', context=my_playlists)
However, in the code below the third argument used list name in strings and list name as a variable. Can you please help me to understand what is my_playlists (in strings) and the second my_playlists (variable). Why so?
from django.shortcuts import render
my_playlists=[
{"id":1,"name":"Car Playlist","numberOfSongs":4},
{"id":2,"name":"Coding Playlist","numberOfSongs":2}
]
def home(request):
return render(request,'zing_it/home.html',{"my_playlists":my_playlists})
Answers:
You can pass the dictionary as context as well.
On your case context = {"my_playlists":my_playlists}
You can add context as a variable first and then assign the value to render function like below:
from django.shortcuts import render
my_playlists = [
{"id": 1, "name": "Car Playlist", "numberOfSongs": 4},
{"id": 2, "name": "Coding Playlist", "numberOfSongs": 2}
]
def home(request):
context = {
"my_playlists": my_playlists
}
return render(request, 'zing_it/home.html', context)
You can do the same thing using the inline how you did. You can update it with:
from django.shortcuts import render
my_playlists=[
{"id":1,"name":"Car Playlist","numberOfSongs":4},
{"id":2,"name":"Coding Playlist","numberOfSongs":2} ]
def home(request):
return render(request,'zing_it/home.html',context={"my_playlists":my_playlists})
As you can see there are many ways to do the same thing but it’s up to you which one you like but better to follow the same pattern to maintain code easily.
Also, in the context this is key value pair as it’s a dictionary. The first on left side is the key and right one is the value. Left which is a Key that will be used on your templates to access the model data or any data you want to have there as per your need.You can pass multiple key:value via context.
context
must be a dictionary. It cannot be a list, so the first version won’t work assuming my_playlist
is the same in both examples. Your second version works because it passes a dictionary correctly.
I am confused with Django renderring. I usually used context and dictionary in the third argument:
return render(request,'zing_it/home.html', context=my_playlists)
However, in the code below the third argument used list name in strings and list name as a variable. Can you please help me to understand what is my_playlists (in strings) and the second my_playlists (variable). Why so?
from django.shortcuts import render
my_playlists=[
{"id":1,"name":"Car Playlist","numberOfSongs":4},
{"id":2,"name":"Coding Playlist","numberOfSongs":2}
]
def home(request):
return render(request,'zing_it/home.html',{"my_playlists":my_playlists})
You can pass the dictionary as context as well.
On your case context = {"my_playlists":my_playlists}
You can add context as a variable first and then assign the value to render function like below:
from django.shortcuts import render
my_playlists = [
{"id": 1, "name": "Car Playlist", "numberOfSongs": 4},
{"id": 2, "name": "Coding Playlist", "numberOfSongs": 2}
]
def home(request):
context = {
"my_playlists": my_playlists
}
return render(request, 'zing_it/home.html', context)
You can do the same thing using the inline how you did. You can update it with:
from django.shortcuts import render
my_playlists=[
{"id":1,"name":"Car Playlist","numberOfSongs":4},
{"id":2,"name":"Coding Playlist","numberOfSongs":2} ]
def home(request):
return render(request,'zing_it/home.html',context={"my_playlists":my_playlists})
As you can see there are many ways to do the same thing but it’s up to you which one you like but better to follow the same pattern to maintain code easily.
Also, in the context this is key value pair as it’s a dictionary. The first on left side is the key and right one is the value. Left which is a Key that will be used on your templates to access the model data or any data you want to have there as per your need.You can pass multiple key:value via context.
context
must be a dictionary. It cannot be a list, so the first version won’t work assuming my_playlist
is the same in both examples. Your second version works because it passes a dictionary correctly.