django admin 自定义界面时丢失左侧导航 nav_sidebar

只显示了自定义模板的内容,左侧导航没有显示出来。

原因:context 漏掉了,要补上。

复制代码
# 错误写法(左侧导航不显示)

    def changelist_view(self, request, extra_context=None):
        form = CsvImportForm()
        payload = {"form": form}
        return TemplateResponse(request, "admin/csv_form.html", payload)




# 正确写法:========================

    def changelist_view(self, request, extra_context=None):
        form = CsvImportForm()# 我要用的内容


        # 引入原有的内容
        context = dict(
            # Include common variables for rendering the admin template.
            self.admin_site.each_context(request),  # side nav was not loading because this was not added.
            # Anything else you want in the context...
            # key=value,

            form= form        # 加上我要用的内容

        )

        return TemplateResponse(request, "admin/csv_form.html", context)

How to add left sidebar in custom django admin template - Stack Overflow

相关推荐
会编程的吕洞宾20 分钟前
Spring AI 2.0 工具调用实战:让 AI 自动调用你的 Java 方法
java·人工智能·经验分享·后端·spring
技术小代20 分钟前
弹窗里的 RegionName 为什么不生效?把区域作用域和模块化一次讲明白|MVVM框架-Prism-3
后端
斯维赤23 分钟前
SELECT * 为什么是覆盖索引的天敌?
java·后端·mysql
炸薯条!1 小时前
Python面向对象基础:类与对象、实例方法、魔法方法和属性详解
java·开发语言·python
程序员cxuan1 小时前
上海团队开源了首个国产记忆模型,554B!这也太狠了
人工智能·后端·程序员
天天鸭2 小时前
前端转全栈,要学会后端看项目的思维
前端·后端·ai编程
BingoGo2 小时前
为什么 PHP 应该只保留 ZTS 构建模式
后端·php
tryCbest2 小时前
Python 爬虫从入门到实战(二):进阶篇——反爬对抗、并发与动态页面
开发语言·爬虫·python
VIP_CQCRE2 小时前
GPT Image 2.6 动态表情交付别只发预览:GIF 文件校验、哈希清单与下载排错
图像处理·人工智能·python·gif
2601_967987372 小时前
使用 NumPy 和 Python 实现梯度下降
python·机器学习·线性回归·numpy·梯度下降