Can Multiple QuerySet Examples be in Same Page View

Say I want to produce a page like Django QuerySet - Get Data .

I could give Django QuerySet - Get Data page a view name of queryset_get .

I want to have examples like The values() Method and Return Specific Columns showing in that same page.

W3Schools uses a testing view for both examples like this

The values() Method Example

from django.http import HttpResponse
from django.template import loader
from .models import Member

def testing(request):
  mydata = Member.objects.all().values()
  template = loader.get_template('template.html')
  context = {
    'mymembers': mydata,
  }
  return HttpResponse(template.render(context, request))

Return Specific Columns Example

from django.http import HttpResponse
from django.template import loader
from .models import Member

def testing(request):
  mydata = Member.objects.values_list('firstname')
  template = loader.get_template('template.html')
  context = {
    'mymembers': mydata,
  }
  return HttpResponse(template.render(context, request))

Both testing views for these examples have same context with 'mymembers': mydata, but use different methods for getting data.

Is it possible that I can use both examples with same queryset_get view with this Django QuerySet - Get Data page ?

What do you mean by “use”? (You do have the issue here that both functions have the same name.)

A view is a function that receives a request and returns an HttpResponse.

What it does between those two events is up to you. It can call other functions.

However, keep in mind that it’s only the view that has the responsibility of returning the response.

Side note: Find a better tutorial. Look for materials worth spending time on in the Educational section of the Awesome Django page.

I could have a view like this for Django QuerySet - Get Data page.

def queryset_get(request):
  template = loader.get_template('testing/queryset_get.html')
  return HttpResponse(template.render())

Then I have to work out how I can these examples in same queryset_get view.
The values() Method Example and Return Specific Columns Example

Below, I have have them both using same view.
I have used mydata and mydata2, so that 2 examples can be used in same page.

def queryset_get(request):
  mydata = Member.objects.all().values()
  mydata2 = Member.objects.values_list('firstname')
  template = loader.get_template('testing/queryset_get.html')
  context = {
    'mymembers': mydata, mydata2,
  }
  return HttpResponse(template.render(context, request))

Hopefully this makes sense.

I am just wondering what are the things that you do not like about W3Schools Tutorials.
I get the impression from what you have said to me before that it is to do with it not being up to date.
Is there any thing else apart from that though ?

It’s been a long time since I’ve looked at it directly, so I can’t really identify any specifics.

Mostly, my opinion ends up being formed from people coming here with questions after trying to learn from it.

What I find is that in addition to some out-of-date material, they seem to be misunderstanding some fundamental concepts about how Django works.

Either they get the wrong ideas about how some things should be done, or believe some things are necessary when they’re not, or have their thinking “limited” or “directed” certain ways that are counter-productive. Or, there are simply some crucial pieces of information that weren’t covered at all, or at least not in a way that the individual recognized the importance of it.

Now, I admit it may have just been those individuals. They may have been coming from different backgrounds such that the site just doesn’t work for them.

However, regardless of the reasons, I have drawn the conclusion that it’s not a good resource - and since there are so many other resources that have a much better track record, I see no reason to recommend it.

If I may be so bold, in your case, you seem to be struggling with the concept of the context in the rendering process. What the root issue behind that is, I couldn’t guess. But while some of it appears it may be related to a lack of understanding of what Python dicts, lists, and objects are, I’d be inclined to believe that the tutorial you’re trying to learn from has failed to emphasize those fundamentals and how they relate to templates.

I have managed to get examples showing on all pages for queryset section.

For example with my http://127.0.0.1:8000/testing/queryset_filter page it displays like this

I use more than one object along with its variable within context to get values like this.
I have first 2 examples on this page shown in views.py below

def queryset_filter(request):
     mydata_filter = Member.objects.filter(firstname='Emil').values()
     mydata_filterand = Member.objects.filter(lastname='Refsnes', id=2).values()
     template = loader.get_template('testing/queryset_filter.html')
     context = {
          # Queryset straight filter method
          'mymembers_filter': mydata_filter,
          # Queryset filter and method
         mymembers_filterand': mydata_filterand,
         }
         return HttpResponse(template.render(context, request))

In html queryset_fliter template, I have used code like this to display a filter example

<h4>QuerySet Filter Method Example</h4>

      <p>Display only records where firstname is 'Emil' :</p>

      <p>Queryset object :</p>

      {{ mymembers_filter }} 

      <p>Loop through items :</p>

      <table border='1'>
         <tr>
            <th>ID</th>
            <th>Firstname</th>
            <th>Lastname</th>
         </tr>
         {% for x in mymembers_filter %} 
            <tr>
               <td>{{ x.id }}</td>
               <td>{{ x.firstname }}</td>
               <td>{{ x.lastname }}</td>
            </tr>
         {% endfor %}
      </table>

So doing it this way worked and you can also use return render in views.py like this
return render(request, 'testing/queryset_filter.html', context)

So I am happy that this is now working.
I would now like to discuss with you how I can get other more complicated examples displaying on other pages, but on my Django Code Result not Displaying with include post.

Thanks
Robert