Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有get_queryset()的基于Django类的视图_Django - Fatal编程技术网

带有get_queryset()的基于Django类的视图

带有get_queryset()的基于Django类的视图,django,Django,我想在基于类的视图中使用get\u queryset(),但我无法在模板中显示变量 功能非常简单: class MyClassView(LoginRequiredMixin, ListView) : template_name = 'my_template.html' model = Foo def get_queryset(self) : foo = Foo.objects.order_by('-id') bar = foo.fil

我想在基于类的视图中使用
get\u queryset()
,但我无法在模板中显示变量

功能非常简单:

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
和我的模板:

<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>

身份证件
{foo%中项目的%s}
{{foo.id}
{%endfor%}


身份证件 {%条中的项目为%} {{bar.id}} {%endfor%}

我必须使用
Context
dictionary?

用于
列表视图的
get\u queryset
方法应返回单个queryset(或项目列表)。如果要向上下文中添加另一个变量,请同时重写
get\u context\u data

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

列表视图
的模板中,您可以使用
对象列表
对象列表
(例如
对象列表
)访问查询集。我在
get\u context\u data
中使用了
bar\u list
来与之保持一致。您需要将模板更改为通过
{%foritem in foo_list%}
{%foritem in bar_list%}

循环
列表视图的
get_queryset
方法应返回单个查询集(或项目列表)。如果要向上下文中添加另一个变量,请同时重写
get\u context\u data

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
列表视图
的模板中,您可以使用
对象列表
对象列表
(例如
对象列表
)访问查询集。我在
get\u context\u data
中使用了
bar\u list
来与之保持一致。您需要将模板更改为循环浏览
{%forfoo_list%}
中的项目和
{%forbar_list%}

两件事:

1)
get\u queryset
返回一个
queryset
。你正在返回一个元组。 2) 默认情况下,
Listview
将查询集传递到名为
object\u list

但是,您可以在模板中完成这一切,对方法的重写为零

{% for item in object_list %}
    {% if item.country == 64 %}
    <tr>
      <td>{{ item.id }}</td>
    </tr>
   {% endif %}
{% endfor %}
{%用于对象列表%中的项]
{%if item.country==64%}
{{item.id}
{%endif%}
{%endfor%}
这将迭代所有项目
Foo
,并且只打印那些具有
country==64
的项目(如果是外键,则需要以不同的方式构造查询)

如果出于某种原因必须在视图中执行此操作,则需要通过
get\u queryset
get\u context
调整,以获得两个不同的对象列表。

两件事:

1)
get\u queryset
返回一个
queryset
。你正在返回一个元组。 2) 默认情况下,
Listview
将查询集传递到名为
object\u list

但是,您可以在模板中完成这一切,对方法的重写为零

{% for item in object_list %}
    {% if item.country == 64 %}
    <tr>
      <td>{{ item.id }}</td>
    </tr>
   {% endif %}
{% endfor %}
{%用于对象列表%中的项]
{%if item.country==64%}
{{item.id}
{%endif%}
{%endfor%}
这将迭代所有项目
Foo
,并且只打印那些具有
country==64
的项目(如果是外键,则需要以不同的方式构造查询)


如果出于某种原因,您必须在视图中执行此操作,则需要通过
get_queryset
get_context
调整两个不同的对象列表。

by OP logic可能是
context['bar']=self.get_queryset().filter(Country=64)
?谢谢您的回答。但是我没有克服在模板中显示foo的困难,我不明白为什么?你可能在模板中使用了错误的变量。如果在视图和模板中使用了虚构的变量名,如
foo
bar
,则更难提供帮助。@BearBrown我已删除了重复的
foo.objects
,但是从上下文中获取了查询集,而不是再次调用
get\u queryset
。我只是遇到了一个问题:
异常类型:keyrerror at/Foo/List/Exception值:context['bar\u List']=context['Foo\u List']=context['Foo\u List'].过滤器(Country=64)
按操作逻辑可能是
context['bar]=self.get\u queryset().过滤器(Country=64)
?谢谢您的回答。但是我没有克服在模板中显示foo的困难,我不明白为什么?你可能在模板中使用了错误的变量。如果在视图和模板中使用了虚构的变量名,如
foo
bar
,则更难提供帮助。@BearBrown我已删除了重复的
foo.objects
,但是从上下文中获取了查询集,而不是再次调用
get\u queryset
。我只是遇到了一个问题:
Exception Type:keyrerror at/Foo/List/Exception Value:context['bar\u List']=context['Foo\u List'].filter(Country=64)