Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7405399
  • 博文数量: 1756
  • 博客积分: 18684
  • 博客等级: 上将
  • 技术积分: 16232
  • 用 户 组: 普通用户
  • 注册时间: 2010-06-02 10:28
个人简介

啥也没写

文章分类

全部博文(1756)

文章存档

2024年(2)

2023年(44)

2022年(39)

2021年(46)

2020年(43)

2019年(27)

2018年(44)

2017年(50)

2016年(47)

2015年(15)

2014年(21)

2013年(43)

2012年(143)

2011年(228)

2010年(263)

2009年(384)

2008年(246)

2007年(30)

2006年(38)

2005年(2)

2004年(1)

分类: Python/Ruby

2023-02-16 14:48:02

通过changelist_view来指定模板

点击(此处)折叠或打开

  1. class CostAdmin(admin.ModelAdmin):
  2.     list_display = ("id", "idcname", "costnum", "content", "createdate")
  3.     list_per_page = 10

  4.     def changelist_view(self, request, extra_context=None):
  5.         # Aggregate new subscribers per day
  6.         #print(request.GET)
  7.         this_year = request.GET.get("year")
  8.         if not this_year:
  9.             this_year = time.strftime("%Y", time.localtime(time.time()))
  10.         year_data = (
  11.             Cost.objects.filter(createdate__year=this_year).extra(select={"year":"strftime('%%Y',cost.createdate)"}).values("year","idcname").annotate(sum_costnum=Sum("costnum")).values("year","idcname__name","sum_costnum").order_by()
  12.         )

  13.         extra_context = extra_context or {"month_data": month_data, "year_data":year_data}

  14.         # Call the superclass changelist_view to render the page
  15.         return super().changelist_view(request, extra_context=extra_context)

  16.     #添加新的URL
  17.     def get_urls(self):
  18.         urls = super().get_urls()
  19.         extra_urls = [
  20.             path("chart_data/", self.admin_site.admin_view(self.chart_data_endpoint))
  21.         ]
  22.         return extra_urls + urls

  23.     def chart_data_endpoint(self, request):
  24.         chart_data = [1,2,3,4,5,6]
  25.         return JsonResponse(list(chart_data), safe=False)

  26. admin.site.register(Cost, CostAdmin)
模板路径
templates/admin/cost/cost/change_list.html 
-rw-r--r-- 1 root root 2740 Feb  7 09:38 templates/admin/cost/cost/change_list.html

点击(此处)折叠或打开

  1. {% load static %}

  2. {% block extrahead %}
  3. <link rel="stylesheet" href="http s://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" cros
  4. sorigin="anonymous">
  5. <script type="text/javascript" src="http s://cdn.staticfile.org/echarts/5.3.3/echarts.min.js"></script>
  6. <script src="http s://unpkg.com/vue@next"></script>
  7. <script src="http s://unpkg.com/axios@1.1.2/dist/axios.min.js"></script>

  8. {% endblock %}

  9. {% block content %}
  10. <div id="app">
  11.         ${ message }<br>
  12.          <button @click="count++">${ count }+</button>
  13.          <button @click="count--">${ count }-</button>
  14. </div>

  15. <table border="1">
  16.   <thead>
  17.   <tr>
  18.     <th></th>
  19.     <th>供应商</th>
  20.         <th>金额</th>
  21.   </tr>
  22.   </thead>
  23.   <tbody>
  24.   {% for i in year_data %}
  25.   <tr>
  26.     <td>{{i.year}}</td>
  27.     <td>{{i.idcname__name}}</td>
  28.     <td>{{i.sum_costnum}}</td>
  29.   </tr>
  30.   {% endfor %}
  31.   </tbody>
  32. </table>
  33. <hr>

  34. {{ block.super }}
  35. <hr>

  36.     <div id="main" style="width:100%;height:400px;"></div>
  37.     <script type="text/javascript">
  38.         // 基于准备好的dom,初始化echarts实例
  39.         var myChart = echarts.init(document.getElementById('main'));
  40.  
  41.         // 指定图表的配置项和数据
  42.         var option = {
  43.             title: {
  44.                 text: '每月统计'
  45.             },
  46.             tooltip: {},
  47.             label: {
  48.                 show: false,
  49.             },
  50.             legend: {
  51.                 data:{{ legend|safe }},
  52.                 right: '4%',
  53.             },
  54.             xAxis: {
  55.                 data: {{ axis|safe }}
  56.             },
  57.             yAxis: {
  58.                 type: 'log',
  59.                 min:1,
  60.                 logBase:10
  61.             },
  62.             series: {{ result|safe }}
  63.         };
  64.  
  65.         // 使用刚指定的配置项和数据显示图表。
  66.         myChart.setOption(option);


  67.         const HelloVueApp = {
  68.                 compilerOptions: {
  69.             delimiters: ['${', '}'],
  70.             comments: true
  71.         },
  72.                 data() {
  73.                 return {
  74.                         message: 'Hello Vue!!',
  75.                                 count: 0,
  76.                 }
  77.                 },
  78.                 mounted () {
  79.                         axios.get('/admin/cost/cost/chart_data/')
  80.                         .then(response => (this.message = response.data))
  81.                         .catch(function(error){console.log(error)});
  82.                 }
  83.                 }
  84.                 Vue.createApp(HelloVueApp).mount('#app');
  85.         
  86.     </script>
  87.     <script src="http s://cdn.staticfile.org/jquery/1.12.4/jquery.min.js" ></script>
  88.     <script src="http s://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js" integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd" crossorigin="anony
  89. mous"></script>


  90. {% endblock %}


阅读(334) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~