1

I would like to use get_queryset() with Class Based View but I don't overcome to display variables in my template.

The function is very simple :

class MyClassView(LoginRequiredMixin, ListView) :

    template_name = 'my_template.html'
    model = Foo

    def get_queryset(self) :

        foo = Foo.objects.order_by('-id')
        bar = foo.filter(Country=64)
        return foo, bar

And my template :

<table style="width:120%">
                <tbody>
                <tr>
                    <th>ID</th>
                </tr>
                {% for item in foo %}
                <tr>
                    <td>{{ foo.id }}</td>
                </tr>
                {% endfor %}
                </tbody>
            </table>
<br></br>
<table style="width:120%">
                <tbody>
                <tr>
                    <th>ID</th>
                </tr>
                {% for item in bar %}
                <tr>
                    <td>{{ bar.id }}</td>
                </tr>
                {% endfor %}
                </tbody>
            </table>

I have to use Context dictionary ?

2 Answers 2

16

The get_queryset method for a ListView should return a single queryset (or list of items). If you want to add another variable to the context, then override get_context_data as well:

class MyClassView(LoginRequiredMixin, ListView) :

    template_name = 'my_template.html'
    model = Foo

    def get_queryset(self) :
        queryset = Foo.objects.order_by('-id')
        return queryset

    def get_context_data(self, **kwargs):
        context = super(MyClassView, self).get_context_data(**kwargs)
        context['bar_list'] = context['foo_list'].filter(Country=64)
        return context

In the template for a ListView, you access the queryset with object_list or <model>_list (e.g. foo_list), unless you set context_object_name. I've used bar_list in get_context_data to be consistent with that. You need to change the template to loop through {% for item in foo_list %} and {% for item in bar_list %}

Sign up to request clarification or add additional context in comments.

6 Comments

by OP logic may be context['bars'] = self.get_queryset().filter(Country=64)?
Thank you for your answer. But I don't overcome to display foo in my template and I don't understand why ?
You're probably using the wrong variable in your template then. It's harder to help when you've used made up variable names like foo and bar in your view and template.
@BearBrown I've removed the repeated Foo.objects, but fetched the queryset from the context instead of calling get_queryset again.
I'm just getting an issue : Exception Type: KeyError at /Foo/List/ Exception Value: 'foo_list' in context['bar_list'] = context['foo_list'].filter(Country=64)
|
1

Two things:

1) get_queryset returns, well, a QuerySet. You're returning a tuple. 2) Listview by default passes the query set into a template variable called object_list

However, you can do this all in the template with zero overrides to methods.

{% for item in object_list %}
    {% if item.country == 64 %}
    <tr>
      <td>{{ item.id }}</td>
    </tr>
   {% endif %}
{% endfor %}

That would iterate over all items Foo and only print ones that have country == 64 (If that's a foreign key, you'd need to construct the query differently.)

If for some reason you must do this in the view, you'll need to tweak by get_queryset and get_context to have two different object lists.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.