【问题标题】:Display Charts based on year select in django根据 django 中的年份选择显示图表
【发布时间】:2021-10-26 11:39:58
【问题描述】:

我使用 django=3.0.0 作为我的后端。我的数据库中有多年的日期时间字段,我想把它作为我的选择按钮的一个选项,它将在 chartjs 中动态更改图表。 我是否必须为此做一个过滤器,或者有没有其他方法可以将我的岁月放在 chartjs 中。 谁能指导我如何做到这一点?

【问题讨论】:

标签: python django database select chart.js


【解决方案1】:

好吧,这是一个例子。

# views.py

class SomeView(View):
   def get(self, request, *args, **kwargs):
      context = {} # put some data here
      return render(request, "pathto/jsfile.js", context)
# urls.py

urlpatterns = [
   # ...
   path("chart/js", views.SomeView.as_view(), name="js"),
   # ...
]
# template
<!-- example -->
<div>
   <canvas id="chart"></canvas>
   <script type="text/javascript" src="{% url 'js' %}"></script>
<div/>
# jsfile.js

var chart = document.getElementById("chart");

{% if some_context %}
   // do something 
{% endif %}

【讨论】:

    猜你喜欢
    • 2018-03-02
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-24
    • 2020-06-03
    • 1970-01-01
    相关资源
    最近更新 更多