How I can use ruls in template class views?

I use chart js as the same steps in this way from in the web page - django-chartjs 1.5.0 : https://pypi.org/project/django-chartjs/
but I can’t know how I use urls to connect with the html
the example says like that:
views.js
class LineChartJSONView(BaseLineChartView):
def get_labels(self):
“”“Return 7 labels for the x-axis.”""
return [“January”, “February”, “March”, “April”, “May”, “June”, “July”]

def get_providers(self):
    """Return names of datasets."""
    return ["Central", "Eastside", "Westside"]

def get_data(self):
    """Return 3 datasets to plot."""

    return [[75, 44, 92, 11, 44, 95, 35],
            [41, 92, 18, 3, 73, 87, 92],
            [87, 21, 94, 3, 90, 13, 65]]

line_chart = TemplateView.as_view(template_name=‘president/line_chart.html’)
line_chart_json = LineChartJSONView.as_view()
but appea in this error


Make sure to include all relevant parts of code if you ask for help. The HTML here is very crucial in diagnosing the issue. I had to do the tutorial quickly to figure out what was going on… but you should be able to fix your implementation as follows:

1.) Uncomment the second to last path, you need both. The commented one is the Ajax view that is serving the data to the other view… can’t view the data if you don’t serve it lol.

2.) I’m gonna assume you haven’t changed the html given in the startup tutorial for the change I propose. Notice the following line:

$.get('{% url "line_chart_json" %}', function(data) {

Ajax is making a get request to the URL with the name “line_chart_json” which in your implementation DNE. Change the line you just uncommented to:

path('line_chart/', LineChartJSONView.as_view(template_name="president/line_chart.html", name="line_chart_json")

If you changed the name of the {% url ‘line_chart_json’ %} within the HTML you will need to change the name argument to align with the new value.

For reference, here’s my working solution:

Views.py

from random import randint
from django.views.generic import TemplateView
from chartjs.views.lines import BaseLineChartView


class LineChartJSONView(BaseLineChartView):
    def get_labels(self):
        """Return 7 labels for the x-axis."""
        return ["January", "February", "March", "April", "May", "June", "July"]

    def get_providers(self):
        """Return names of datasets."""
        return ["Central", "Eastside", "Westside"]

    def get_data(self):
        """Return 3 datasets to plot."""

        return [[75, 44, 92, 11, 44, 95, 35],
                [41, 92, 18, 3, 73, 87, 92],
                [87, 21, 94, 3, 90, 13, 65]]

URLS.py

from django.urls import path
from django.views.generic import TemplateView
from . import views

urlpatterns = [
    path('dash/', TemplateView.as_view(template_name='line_chart.html')),
    path('ajax/', views.LineChartJSONView.as_view(), name="line_chart_json")
]

Take note that I imported as “from . import views” so i need to use views.MY_VIEW instead of just MY_VIEW as you use when importing directly.

Can’t believe I just asked you to remember to include the HTML and forgot mine… oof.

HTML

{% load staticfiles %}
<html>
    <head>
        <title>django-chartjs line chart demo</title>
        <!--[if lte IE 8]>
            <script src="{% static 'js/excanvas.js' %}"></script>
        <![endif]-->
    </head>
    <body>
        <h1>Some Line Charts loaded in Ajax!</h1>

        <canvas id="myChart" width="500" height="400"></canvas>

        <script type="text/javascript" src="http://code.jquery.com/jquery-1.10.0.min.js"></script>
        <script type="text/javascript" src="{% static 'js/Chart.min.js' %}"></script>
        <script type="text/javascript">
            $.get('{% url "line_chart_json" %}', function(data) {
                var ctx = $("#myChart").get(0).getContext("2d");
                new Chart(ctx, {
                    type: 'line', data: data
                });
            });
        </script>
    </body>
</html>