【问题标题】:Django - Admin - How to override change_list template for Model Proxy?Django - 管理员 - 如何覆盖模型代理的 change_list 模板?
【发布时间】:2011-07-26 04:20:19
【问题描述】:

我制作了一个简单的 Django 应用程序。我有一个模型“访客”。我的目标是在 Django 管理员中出现两个表。一个包含所有访客,另一个包含今天的访客。

通过关注these instructions,我得到了使用以下代码的所有功能。但我不确定如何只为 VisitorExpectedTodayProxy 覆盖 change_list.html。

我尝试关注 instructions here 并创建了 Site/templates/admin/VisitorLog/VisitorExpectedTodayProxy/change_list.html 并在那里进行了更改,但它似乎没有成功。

Models.py

class Visitor(models.Model):
    visit_datetime = models.DateTimeField(null=True)
    visitor_name = models.CharField(max_length=500)

#Make dummy models for different object views in admin interface
class VisitorExpectedTodayProxy(Visitor):
    class Meta:
        proxy=True
        verbose_name = "Visitor"
        verbose_name_plural = "Today's Visitors and Regular Visitors"

【问题讨论】:

    标签: python django django-admin django-templates


    【解决方案1】:

    除了小写你的路径看起来像:

    templates/admin/visitorlog/visitorexpectedtodayproxy/change_list.html
    

    你的 change_list.html 的内容应该扩展默认的 admin/change_list:

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

    您可以通过查看 django 源代码中的各种块模板来自定义此页面的不同部分:

    django/contrib/admin/templates/admin/change_list.html
    

    【讨论】:

    • 我只是将 change_list.html 复制到目录中并在那里进行了修改。有没有更好的办法?
    【解决方案2】:

    尝试将文件夹名称小写。

    请注意,管理应用程序将 查找时将型号名称小写 对于目录,所以请确保您 以全小写命名目录 if 你将在一个 区分大小写的文件系统。

    另外,您是否检查过确保您的模板目录正常工作的清单? TEMPLATE_DIRS 需要包含你的这个模板文件夹,并确保filesystem 模板加载器在app_directories 加载器之前。

    最后,您可以通过 ModelAdmin 属性指向模板: http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.change_list_template

    【讨论】:

      猜你喜欢
      • 2013-01-23
      • 1970-01-01
      • 2018-10-22
      • 2019-05-02
      • 2010-11-19
      • 2018-02-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-12
      相关资源
      最近更新 更多