Django and ChartJS

后端 未结 3 697
醉话见心
醉话见心 2021-02-09 00:53

I\'m trying to understand if it it\'s possible to incorporate dynamic data into a Django Chart JS architecture. I went through a couple of tutorials and ultimately got Django t

3条回答
  •  天命终不由人
    2021-02-09 01:21

    My code below is what ultimately solved the problem on how to get dynamic user data. I am still trying to figure out how to get the labels to work properly. This solution allows the labels to display with ID numbers, which isn't optimal, but I opened a separate SO for the label issue.

    My HTML

       {% extends 'base5.html' %}
    
    {% block body_block %}
    

    Number of Books By Author

    {% endblock %}

    Views.py

    ChartView

    class ChartData(LoginRequiredMixin,APIView):
        model = Author
        authentication_classes = (SessionAuthentication, BasicAuthentication)
        permission_classes = (IsAuthenticated,)
    
        def get(self, request, format=None):
            default_items = []
            labels = []
            data = {
                "labels": labels,
                "default": default_items,
            }
            return Response(data)
    

    The ChartView

    class ChartView(LoginRequiredMixin, TemplateView): template_name = 'Book/chart.html'
    
    def get_context_data(self, **kwargs):
        context = super(ChartView, self).get_context_data(**kwargs)
        book = Author.objects.filter(id__in=self.request.user.userprofile.author.all()).order_by('id')
        books_count = [ Book.objects.filter(author=cls).count() for cls in book ]
        context['book'] = book
        context['book_count'] = books_count
        return context
    

提交回复
热议问题