Python 无法将变量从admin.py导出到django中的重写模板
晚安,我在django(change_list.html)中重写了一个模板,以便在中的教程之后在django管理页面中显示chart.js图表,但是当我到达零件时,我必须用admin.py文件中的变量chart_数据替换硬编码数据,并将其传递到重写的模板change_list.html,没有发生任何情况,表中显示的所有值均为1。可能发生的情况是,我没有找到任何关于如何将变量从admin.py传递到覆盖的模板html文件的信息。这是密码Python 无法将变量从admin.py导出到django中的重写模板,python,html,django,charts,Python,Html,Django,Charts,晚安,我在django(change_list.html)中重写了一个模板,以便在中的教程之后在django管理页面中显示chart.js图表,但是当我到达零件时,我必须用admin.py文件中的变量chart_数据替换硬编码数据,并将其传递到重写的模板change_list.html,没有发生任何情况,表中显示的所有值均为1。可能发生的情况是,我没有找到任何关于如何将变量从admin.py传递到覆盖的模板html文件的信息。这是密码 class ReporteActividadUsuarios
class ReporteActividadUsuariosAdmin(admin.ModelAdmin):
list_display = ['nombre','apellidos','carnet','cliente','cantidad_de_prestamos']
ordering = ['-cantidad_de_prestamos']
def changelist_view(self, request, extra_context=None):
chart_data = (
ReporteActividadUsuarios.objects.annotate(Count("cantidad_de_prestamos"))
.values("nombre","apellidos")
.annotate(y=Count("cantidad_de_prestamos"))
#.group_by("cantidad_de_prestamos")
#.order_by("-carnet")
)
# Serialize and attach the chart data to the template context
as_json = json.dumps(list(chart_data), cls=DjangoJSONEncoder)
extra_context = extra_context or {"chart_data": as_json}
# Call the superclass changelist_view to render the page
return super().changelist_view(request, extra_context=extra_context)
def has_add_permission(self, request):
return False
覆盖的模板文件change_list.html如下所示:
{% extends "admin/change_list.html" %}
{% load static %}
{% block content %}
{% block extrahead %}
{{ block.super }}
<link rel="stylesheet" href="{% static '/asset/css/Chart.min.css' %}" />
<script src="{% static '/asset/js/admin/Chart.bundle.min.js' %}"></script>
<script>
document.addEventListener('DOMContentLoaded', () => {
const ctx = document.getElementById('myChart').getContext('2d');
const chartData = {{ chart_data | safe }};
//const chartData = [1,7,12,8,2] ;
// Render the chart
var myPieChart = new Chart(ctx, {
type: 'bar',
data: {
datasets: [{
data: chartData,
borderColor: '#00443436' ,
backgroundColor: ['#df0000','lightgreen','orange','lightblue','lightyellow'],
}],
// These labels appear in the legend and in the tooltips when hovering different arcs
labels: [
'MARIA DEL CARMEN MUZIO ZARRANZ',
' LAY MI RODRIGUEZ GUILBEAUX',
'REINALDO LAZARO LASTRE LABRADA',
'SISSI CARIDAD RODRIGUEZ PERAZA',
'AUSTIN LLERANDI PEREZ'
]
},
options: {
//animation: { animateRotate: true,} ,
}
});
}) ;
</script>
{% endblock %}
<h3> Usuarios Mas Activos </h3>
<div style="width: 23%;">
<canvas style="margin-bottom: 0px; width: 65%; height: 60%;"
id="myChart"></canvas>
</div>
<!-- Render the rest of the ChangeList view by calling block.super -->
{{ block.super }}
{% endblock %}
{%extends“admin/change\u list.html”%}
{%load static%}
{%block content%}
{%block extrahead%}
{{block.super}}
document.addEventListener('DOMContentLoaded',()=>{
const ctx=document.getElementById('myChart').getContext('2d');
const chartData={{chart_data | safe}};
//常量图表数据=[1,7,12,8,2];
//绘制图表
var myPieChart=新图表(ctx{
类型:'bar',
数据:{
数据集:[{
数据:图表数据,
边框颜色:'#00443436',
背景颜色:['df0000'、'浅绿色'、'橙色'、'浅蓝色'、'浅黄色'],
}],
//当悬停在不同的圆弧上时,这些标签将显示在图例和工具提示中
标签:[
“MARIA DEL CARMEN MUZIO ZARRANZ”,
“我是罗德里格斯·吉尔博”,
“REINALDO LAZARO LASTRE LABRADA”,
“SISSI CARIDAD RODRIGUEZ PERAZA”,
“奥斯汀·勒兰迪·佩雷斯”
]
},
选项:{
//动画:{animateRotate:true,},
}
});
}) ;
{%endblock%}
惯常行为
{{block.super}}
{%endblock%}
非常提前感谢,为什么
extra_context=extra_context或{“chart_data”:as_json}
?这意味着如果extra\u context
不是None
则永远不会添加图表数据。另外,请查看浏览器开发工具中页面的源代码。您在那里看到了什么图表数据?你看到错误了吗?我看到的唯一一个错误是chartData是空的,JavaScript页面中的代码是chartData=;我的问题也是。JavaScript如何从Django admin“查看”变量图表数据我看到admin.py changeklist_视图类导出了额外的上下文混合,但我仍然无法理解它是如何工作的。它没有看到它。它被传递到上下文,然后使用{{chartData | safe}
在模板中打印它。