Chartjs-How can I process json file and display some data in form of charts in django

1👍

Create a model to upload the file. Create a director upload for uploading at root of your project

from django.db import models

class JSONFile(models.Model):
    file = models.FileField(upload_to='uploads')

You can use a form:

from django import forms

class JSONFileForm(forms.Form):
    file = forms.FileField(
        label='Select a file',
        help_text='max. 42 megabytes'
    )

Create templates as ‘chart.html’ to show your chart accordingly and ‘upload.html’ to render above form

In your views.py:

from forms import JSONFileForm
from models import JSONFile
from django.shortcuts import render
def chart(request):
    if request.method == 'POST':
        form = JSONFileForm(request.POST, request.FILES)
        if form.is_valid():
            newfile = JSONFile(file = request.FILES['file'])
            newfile.save()
            json.dumps(request.FILES['file'])
            #format your data here
            return render(request,"chart.html",{}) #pass data context in {} as dict
        else:
            form = JSONFileForm(request.POST)
    else:
         form = JSONFileForm()
         return render(request,"upload.html",{"form":form})

Leave a comment