As I was mentioning before on my Can Multiple QuerySet Examples be in Same Page View post, I will discuss more difficult examples for displaying example on a Django for Loop page.
I am sure you will remember fruits and cars examples on this post.
Say that I would want to display these examples within a http://127.0.0.1:8000/testing/for_loop_tested page that uses a for_loop_tested.html template
How can I use a similar method like before with queryset examples when I am dealing with a larger array,
like this for cars
context = {
'cars': [
{
'brand': 'Ford',
'model': 'Mustang',
'year': '1964',
},
{
'brand': 'Ford',
'model': 'Bronco',
'year': '1970',
},
{
'brand': 'Volvo',
'model': 'P1800',
'year': '1964',
}]
}
and a fruits array
context = {
'fruits': ['Apple', 'Banana', 'Cherry'],
}
Following my previous example, I thinking that it could be done, like this
def for_loop_tested(request):
fruits_array = ['Apple', 'Banana', 'Cherry']
cars_array = [
{
'brand': 'Ford',
'model': 'Mustang',
'year': '1964',
},
{
'brand': 'Ford',
'model': 'Bronco',
'year': '1970',
},
{
'brand': 'Volvo',
'model': 'P1800',
'year': '1964',
}]
context = {
'fruits': fruits_array,
'cars': cars_array,
}
return render(request, 'testing/for_loop_tested.html', context )
It will be appreciated, if you may please let me know what your thoughts are on this, so that I can get a good understanding on how I can get these more complicated examples displaying on other pages.
Thanks
Robert