Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/83.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
Python Django:如何传递模型&x27;是否将数据保存到管理员默认起始页(index.html)?_Python_Html_Django - Fatal编程技术网

Python Django:如何传递模型&x27;是否将数据保存到管理员默认起始页(index.html)?

Python Django:如何传递模型&x27;是否将数据保存到管理员默认起始页(index.html)?,python,html,django,Python,Html,Django,我已经创建了一个PythonDjango应用程序,并希望在AdminStartPage/defaultpage(index.html)上构建一种仪表板。为此,我想显示我的模型的不同数据 我尝试在管理区之外访问我的模型数据,并按我喜欢的方式显示它们。它在那里奏效了。但是,它在默认起始页的管理区域中不能使用相同的代码 我不知道如何访问index.html/Admin起始页中的模型/对象 非常感谢您的帮助。您可以编写如下自定义模板: # file: my_app/context_processors/

我已经创建了一个PythonDjango应用程序,并希望在AdminStartPage/defaultpage(index.html)上构建一种仪表板。为此,我想显示我的模型的不同数据

我尝试在管理区之外访问我的模型数据,并按我喜欢的方式显示它们。它在那里奏效了。但是,它在默认起始页的管理区域中不能使用相同的代码

我不知道如何访问index.html/Admin起始页中的模型/对象


非常感谢您的帮助。

您可以编写如下自定义模板:

# file: my_app/context_processors/dashboard.py

def dashboard(request):
    return {'model1': Model1.objects.all(), 'model2': Model2.objects.all(), ... }
并将其添加到
模板
中的
context\u处理器
部分:

'OPTIONS': {
        'context_processors': [
            # rest of the processors
            'my_app.context_processors.dashboard'
        ],
 }
最后,在管理站点模板中的任意位置使用它:

{% for model in model1 %}
    {{ model.pk }}
{% endfor %}

谢谢你的帮助。因此,我必须创建一个新的模板“dashboard”,并将其插入到我的视图“index.html”中,对吗?不,您只需循环上下文变量即可访问queryset。我真诚地认为,在管理区域中,模型数据在视图中是可用的,没有任何麻烦。我稍后会尝试并让您知道。谢谢!