将上下文传递到 django 管理索引页面

Goz*_*zie 2 html python django django-admin

我正在尝试在 django admin 的索引页面上创建一个表,列出在特定时间范围内注册的所有用户。我已经设法编辑索引页以包含我的表格,但现在我想填充该表格。我尝试声明extra_context变量,但它仍然没有将其传递给模板。我已经解决这个问题好几天了,但仍然无法弄清楚我做错了什么。这是我的admin.py文件

from django.contrib import admin
from django.contrib.auth.models import Group
from django.contrib.auth.admin import UserAdmin
from .forms import SignUpForm, EditUserForm
from .models import CustomUser
from django.urls import path
from datetime import datetime, timedelta
from django.utils import timezone


class CustomUserAdmin(UserAdmin):
    list_filter = ['created_at',]
    list_display = ['username', 'email', 'created_at', 'is_active']
    # index_template = 'admin/my_index.html'

    def time_threshold(num):
        num = int(num)
        thresh = datetime.now(tz=timezone.utc) - timedelta(hours=num)
        return thresh


    print(time_threshold(30))       
    def get_urls(self):
        urls = super().get_urls()
        custom_urls = [
            path("user_metrics/", self.user_metrics),
        ]
        return custom_urls + urls

    def user_metrics(self, request, extra_context=None):
        extra_context = extra_context or {}
        user_24 = CustomUser.objects.filter(created_at__gt=time_threshold(730))
        extra_context['users_in_past_24_hours'] = CustomUser.objects.filter(created_at__gt=time_threshold(24))
        extra_context['users_in_past_week'] = CustomUser.objects.filter(created_at__gt=time_threshold(168))
        extra_context['users_in_past_month'] = CustomUser.objects.filter(created_at__gt=time_threshold(730))
        print(user_24)
        return super(CustomUserAdmin, self).user_metrics(request, extra_context=extra_context)


admin.site.register(CustomUser, CustomUserAdmin)
admin.site.unregister(Group)
admin.site.site_header = "Savests Custom Admin"
Run Code Online (Sandbox Code Playgroud)

这是我的 index.html 文件,位于 template/admin 文件夹中

{% extends "admin/base_site.html" %}
{% load i18n static %}

{% block extrastyle %}{{ block.super }}<link rel="stylesheet" type="text/css" href="{% static "admin/css/dashboard.css" %}">{% endblock %}

{% block coltype %}colMS{% endblock %}

{% block bodyclass %}{{ block.super }} dashboard{% endblock %}

{% block breadcrumbs %}{% endblock %}

{% block nav-sidebar %}{% endblock %}

{% block content %}
<div id="content-main">
  {% include "admin/app_list.html" with app_list=app_list show_changelinks=True %}
</div>

<div>
    <h5>Users in the last 24 hours</h5>
        <table class="table table-hover table-bordered">
            <caption>User Information</caption>
            <thead class="thead-dark">
                <th>User ID</th>
                <th>Username</th>
                <th>Email Address</th>
                <th>Active Status</th>
                <th>Staff Status</th>
            </thead>
            {% for a in users_in_past_24_hours %}
                <tr>
                    <td>{{ a.id }}</td>
                    <td>{{ a.username }}</td>
                    <td>{{ a.email }}</td>
                    <td>
                        {% if a.is_active %}
                            <form action="{% url 'toggle_user_status_false' user_id=a.id %}" method="post">
                                {% csrf_token %}
                                    <button class="btn btn-danger" type="submit">
                                        Deactivate
                                    </button>
                            </form>
                        {% else %}
                            <form action="{% url 'toggle_user_status_true' user_id=a.id %}" method="post">
                                {% csrf_token %}
                                    <button class="btn btn-primary" type="submit">
                                        Activate
                                    </button>
                            </form>
                        {% endif %}
                    </td>
                    <td>
                        {% if a.is_staff %}
                            Staff
                        {% else %}
                            Not Staff
                        {% endif %}
                    </td>
                </tr>
            {% endfor %}
        </table>
        <br>
        <hr>

        <h5>Users in the past week</h5>
        <table class="table table-hover table-bordered">
            <caption>User Information</caption>
            <thead class="thead-dark">
                <th>User ID</th>
                <th>Username</th>
                <th>Email Address</th>
                <th>Active Status</th>
                <th>Staff Status</th>
            </thead>
            {% for a in users_in_past_week %}
                <tr>
                    <td>{{ a.id }}</td>
                    <td>{{ a.username }}</td>
                    <td>{{ a.email }}</td>
                    <td>
                        {% if a.is_active %}
                            <form action="{% url 'toggle_user_status_false' user_id=a.id %}" method="post">
                                {% csrf_token %}
                                    <button class="btn btn-danger" type="submit">
                                        Deactivate
                                    </button>
                            </form>
                        {% else %}
                            <form action="{% url 'toggle_user_status_true' user_id=a.id %}" method="post">
                                {% csrf_token %}
                                    <button class="btn btn-primary" type="submit">
                                        Activate
                                    </button>
                            </form>
                        {% endif %}
                    </td>
                    <td>
                        {% if a.is_staff %}
                            Staff
                        {% else %}
                            Not Staff
                        {% endif %}
                    </td>
                </tr>
            {% endfor %}
        </table>
        <br>
        <hr>

        <h5>Users in the past month</h5>
        <table class="table table-hover table-bordered">
            <caption>User Information</caption>
            <thead class="thead-dark">
                <th>User ID</th>
                <th>Username</th>
                <th>Email Address</th>
                <th>Active Status</th>
                <th>Staff Status</th>
            </thead>
            {% for a in users_in_past_month %}
                <tr>
                    <td>{{ a.id }}</td>
                    <td>{{ a.username }}</td>
                    <td>{{ a.email }}</td>
                    <td>
                        {% if a.is_active %}
                            <form action="{% url 'toggle_user_status_false' user_id=a.id %}" method="post">
                                {% csrf_token %}
                                    <button class="btn btn-danger" type="submit">
                                        Deactivate
                                    </button>
                            </form>
                        {% else %}
                            <form action="{% url 'toggle_user_status_true' user_id=a.id %}" method="post">
                                {% csrf_token %}
                                    <button class="btn btn-primary" type="submit">
                                        Activate
                                    </button>
                            </form>
                        {% endif %}
                    </td>
                    <td>
                        {% if a.is_staff %}
                            Staff
                        {% else %}
                            Not Staff
                        {% endif %}
                    </td>
                </tr>
            {% endfor %}
        </table>
        <br>
        <hr>
</div>
{% endblock %}

{% block sidebar %}
<div id="content-related">
    <div class="module" id="recent-actions-module">
        <h2>{% translate 'Recent actions' %}</h2>
        <h3>{% translate 'My actions' %}</h3>
            {% load log %}
            {% get_admin_log 10 as admin_log for_user user %}
            {% if not admin_log %}
            <p>{% translate 'None available' %}</p>
            {% else %}
            <ul class="actionlist">
            {% for entry in admin_log %}
            <li class="{% if entry.is_addition %}addlink{% endif %}{% if entry.is_change %}changelink{% endif %}{% if entry.is_deletion %}deletelink{% endif %}">
                {% if entry.is_deletion or not entry.get_admin_url %}
                    {{ entry.object_repr }}
                {% else %}
                    <a href="{{ entry.get_admin_url }}">{{ entry.object_repr }}</a>
                {% endif %}
                <br>
                {% if entry.content_type %}
                    <span class="mini quiet">{% filter capfirst %}{{ entry.content_type.name }}{% endfilter %}</span>
                {% else %}
                    <span class="mini quiet">{% translate 'Unknown content' %}</span>
                {% endif %}
            </li>
            {% endfor %}
            </ul>
            {% endif %}
    </div>
</div>
{% endblock %}
Run Code Online (Sandbox Code Playgroud)

小智 5

第一种方法是声明子类django.contrib.admin.site.AdminSite()并重写.index()中的方法admin.py

class MyAdminSite(django.contrib.admin.site.AdminSite):
    def index(self, request, extra_context=None):
        if extra_context is None:
            extra_context = {}
        user_24 = CustomUser.objects.filter(created_at__gt=time_threshold(730))
        extra_context['users_in_past_24_hours'] = CustomUser.objects.filter(created_at__gt=time_threshold(24))
        extra_context['users_in_past_week'] = CustomUser.objects.filter(created_at__gt=time_threshold(168))
        extra_context['users_in_past_month'] = CustomUser.objects.filter(created_at__gt=time_threshold(730))
        extra_context["app_list"] = get_app_list_in_custom_order()
        return super(MyAdminSite, self).index(request, extra_context)
Run Code Online (Sandbox Code Playgroud)

那么您应该在创建自己的 AdminSite 时注册您的站点管理员(也在 admin.py 中), my_admin_site = MyAdminSite(name='myadmin') 您需要注册使用它的所有模型,因此 admin.site.register(YourModel)您应该使用my_admin_site.register(YourModel)您使用的所有模型。您还需要添加管理站点的路径urls.py

from pathtoyouradmin.admin import my_admin_site


urlpatterns = [
...
    path('admin/', my_admin_site.urls),
...
]
Run Code Online (Sandbox Code Playgroud)

你可以用其他方式解决你的问题。您可以创建contextprocessor检查当前页面路径的位置,如果它管理索引,则提供所需的extra_context

def context_users_info(request):
    extra_context = {}
    if request.path == 'index admin page'# you need to put path to your index admin page
        user_24 = CustomUser.objects.filter(created_at__gt=time_threshold(730))
        extra_context['users_in_past_24_hours'] = CustomUser.objects.filter(created_at__gt=time_threshold(24))
        extra_context['users_in_past_week'] = CustomUser.objects.filter(created_at__gt=time_threshold(168))
        extra_context['users_in_past_month'] = CustomUser.objects.filter(created_at__gt=time_threshold(730))
        extra_context["app_list"] = get_app_list_in_custom_order()
    return extra_context
Run Code Online (Sandbox Code Playgroud)

并且不要忘记在中注册您的上下文处理器settings.py