python - 将 "fake"模型条目添加到管理索引

标签 python django django-templates django-views django-admin

我想添加一个指向管理员索引页面的链接,其中列出了模型,但我不想创建新模型。这将是一个自定义 View 。

这就是我想要的结果。在这种情况下,排行榜应指向/admin/leaderboards代码用户是有效的模型。

desired result

我尝试覆盖index.html页面,根据to the documentation ,使用以下模板。

{% extends "admin/index.html" %}

{% block content %}

    {{ block.super }}

    <div class="app-{{ app.app_label }} module">
    <table>
        <tr>
            <th scope="row"><a href="leaderboards">Leaderboards</a></th>
            <td></td>

        </tr>
    </table>
    </div>

{% endblock %}

但这就是结果:

my approach

我知道我可以替换完整的index.html并在那里硬编码特定行,但很多人不建议这样做。还有其他选择吗?

https://docs.djangoproject.com/en/1.10/ref/contrib/admin/#django.contrib.admin.AdminSite.index_template

最佳答案

Django actually recommendsindex.html 复制粘贴到应用程序文件夹,并硬编码链接。

On a similar note, you might want to customize the look and feel of the Django admin index page.

By default, it displays all the apps in INSTALLED_APPS that have been registered with the admin application, in alphabetical order. You may want to make significant changes to the layout. After all, the index is probably the most important page of the admin, and it should be easy to use.

The template to customize is admin/index.html. (Do the same as with admin/base_site.html in the previous section – copy it from the default directory to your custom template directory). Edit the file, and you’ll see it uses a template variable called app_list. That variable contains every installed Django app. Instead of using that, you can hard-code links to object-specific admin pages in whatever way you think is best.

关于python - 将 "fake"模型条目添加到管理索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40900700/

相关文章:

javascript - 没有点击的第一张图片

python - 为什么 os.kill(pid, 0) 返回 None 尽管进程已经终止?

Django - 全局变量,我应该使用模板上下文处理器吗?

python - 无法在顶级窗口中创建框架

python - 如何使用 python 将 [1,2,3,4] 更改为 '1234'

python - 如何为 python eve-sqlalchemy 编写自定义路由?

python - 检索 Django 中单击的列表项

python - Django 重命名字典值

Django:JSONField + 全文搜索 + 索引 -> 序列扫描。如何配置索引工作?

python - 如何使用 Django 显示毫秒而不是微秒