Django Code Result not Displaying with include

Hi Ken

I have been doing other parts of my Django website recently.

Getting back to this multiple contexts within one view, is there any documentation online that shows code structure for it ?
I understand that I am learning an older W3Schools way by using
return HttpResponse(template.render(context, request))

instead of newer way with
return render(request, template, context)

I want to get used to using older method first as I am following W3Schools tutorial.
However if there is documentation that shows how to do multiple contexts within one view for
return render method as well as return HttpResponse, then once I have it worked out with HttpResponse, I can adjust to newer way.

When you render a template, you only pass it one context. There is no “multiple contexts” within one rendering.

Please, abandon this now. It’s not just “newer” vs “older”. It’s that whatever they’re explaining, it’s been giving people the wrong idea about some topics that is really inhibiting their education. Three of my most unpleasant experiences in this forum have been due to having to explain to people that they need to unlearn some things from that site.
Spend your time on educational materials that are going to present to you the correct concepts.

On your previous reply you showed me this

and mentioned Just include all the code that generates the data in the one view.

What I am asking is, is there documentation for Django that shows how this works for using more than one context for one page that you have shown.

A lot of Django Documentation online uses return HttpResponse and the newer way you have mentioned to me return render I have not seen a lot of documentation on that.

There’s only one context. Context is a dict. The context may contain as many keys as you wish. There’s nothing special, unique, or different about this than any other usage of the context.

What is this then that you showed me.

That’s one context with two keys - ‘fruit’ and ‘cars’

What does context = {} mean at beginning ?
Why is that {} empty

So with context['fruits'] = []

Is this saying that we will use fruits variable as a key and it has an array

Is there any Django documentation on this, is what I am meaning ?

Not Django, Python.

If you are not familiar or comfortable with Python dictionaries, then see 5. Data Structures — Python 3.12.2 documentation

(Even though I’m referring to one specific section on that page, you really do need to be familiar with all of the standard Python data structures.)

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

Yes, this is a valid and appropriate way of handling this.