【问题标题】:Django-simple-history to display logs on webpage other than admin websiteDjango-simple-history 在管理网站以外的网页上显示日志
【发布时间】:2020-04-12 21:56:19
【问题描述】:

我已经成功在管理页面注册了 Django-simple-history。 我现在一直在尝试让审核 (CRUD) 日志显示在管理站点以外的网页上。该页面当前显示为空白。

这是我的尝试 -

views.py 文件

def audit_trail(request, id):
    if request.method == "GET":
        obj = My_Model.history.all(pk=id)
        return render(request, 'audit_trail.html', context={'object': obj})

audit_trail.html 文件


{%extends "did/base.html" %}

{% block content %}

{% for h in object%}
   {{ h.id }}
   {{ h.carrier }}
{% endfor %}

{% endblock content %}

网址格式

path('audit_trail/', views.audit_trail, name='audit_page'),

** 模型文件 **

from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
from simple_history.models import HistoricalRecords

class My_Model(models.Model):
    field1 = models.CharField(max_length=100)
    field2 = models.DateTimeField(default=timezone.now)
    field3 = models.CharField(max_length=100)
    history = HistoricalRecords()

【问题讨论】:

    标签: django django-models django-views django-templates django-simple-history


    【解决方案1】:

    这里的问题是您的查询集没有返回任何内容。历史表上的主键是history_id,而不是来自您的基础My_Model 表的id。因此,如果您将查询更新为 My_Model.history.filter(id=id),这应该可以工作。

    【讨论】:

      猜你喜欢
      • 2020-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-29
      • 1970-01-01
      • 2016-06-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多