Django-Admin-如何覆盖模型代理的更改列表模板?

2024-05-14 20:16:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我做了一个简单的Django应用程序。我有一个模型“访客”。我的目标是在Django管理中显示两个表。一个接待所有的客人,一个只接待今天的客人。

我通过跟踪these instructions获得了下面代码的所有工作。但是我不知道如何覆盖change_list.html来访问expectedtodayproxy。

我试着跟踪instructions here,然后创建了Site/templates/admin/VisitorLog/VisitorExpectedTodayProxy/change_list.html,并在那里进行了更改,但它似乎没有接受。

型号.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"

Tags: djangonametrueverboseadminmodelshtmlchange
2条回答

尝试将文件夹名称小写。

Note, that the admin app will lowercase the model name when looking for the directory, so make sure you name the directory in all lowercase if you are going to run your app on a case-sensitive filesystem.

另外,您是否已经检查了确保模板目录正常工作的检查表?TEMPLATE_DIRS需要包含您的模板文件夹,并确保filesystem模板加载程序位于app_directories加载程序之前。

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

在小写路径的顶部,看起来像:

templates/admin/visitorlog/visitorexpectedtodayproxy/change_list.html

change_list.html的内容应扩展默认的管理/更改列表:

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

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

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

相关问题 更多 >

    热门问题