Missing one positional argument

0 votes
Am trying to retrieve a bar graph to compare my two values for a same month.But I cant achieve this.If mu solution is not right can anyone provide me solution to solve this please?.It throws error here:

@login_required
def chart_view(request,month):
    user = request.user
    try:
        obj = graphinput.objects.get(user=user,Month=month)
    except graphinput.DoesNotExist:
        obj = None
    return render(request,"a.html",{"chart_obj":obj})

MY CODE:

models.py:

from django.db import models
from django.contrib.auth.models import User

class graphinput(models.Model):
  user=models.ForeignKey(User,on_delete=models.CASCADE)
  Month = models.CharField(max_length=30)
  Value1 = models.IntegerField()
  Value2 = models.IntegerField()

html file(a.html):

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

<script src="https://cdn.jsdelivr.net/npm/chart.js@2.9.4"></script>

<script type="text/javascript">
    function create_chart(val1,val2){
        var canvas = document.getElementById('myChart');
        var data = {
            labels: ["Value1", "Value2"],
            datasets: [
                {
                    label: "My Chart",
                    backgroundColor: "rgba(255,99,132,0.2)",
                    borderColor: "rgba(255,99,132,1)",
                    borderWidth: 2,
                    hoverBackgroundColor: "rgba(255,99,132,0.4)",
                    hoverBorderColor: "rgba(255,99,132,1)",
                    data: [val1,val2]
                }
            ]
        };
        var option = {
            animation: {
                duration:5000
            }
        };
        var myBarChart = Chart.Bar(canvas,{data:data,options:option});
    }

    // create chart on window load as it will make sure that chart.js has loaded.
    window.addEventListener('load', (event) => {
        {% if chart_obj %}
        create_chart(chart_obj.Value1,chart_obj.Value2)
        {% else %}
        // This means chart_obj is None.
        create_chart(0,0)
        {% endif %}
    });
</script>

views.py:

from django.shortcuts import render,redirect
from inputs.models import graphinput
from.utils import get_plot
from django.contrib import messages
from django.contrib.auth.decorators import login_required

def index(request):

 if request.method == 'POST':
     JanMonth = request.POST['JanMonth']
     JanValue = request.POST['JanValue']
     JanValue2 = request.POST['JanValue2']

     FebMonth = request.POST['FebMonth']
     FebValue = request.POST['FebValue']
     FebValue2 = request.POST['FebValue2']

     MarMonth = request.POST['MarMonth']
     MarValue = request.POST['MarValue']
     MarValue2 = request.POST['MarValue2']

     AprMonth = request.POST['AprMonth']
     AprValue = request.POST['AprValue']
     AprValue2 = request.POST['AprValue2']

     MayMonth = request.POST['MayMonth']
     MayValue = request.POST['MayValue']
     MayValue2 = request.POST['MayValue2']

     JunMonth = request.POST['JunMonth']
     JunValue = request.POST['JunValue']
     JunValue2 = request.POST['JunValue2']

     JulMonth = request.POST['JulMonth']
     JulValue = request.POST['JulValue']
     JulValue2 = request.POST['JulValue2']

     AugMonth = request.POST['AugMonth']
     AugValue = request.POST['AugValue']
     AugValue2 = request.POST['AugValue2']

     SepMonth = request.POST['SepMonth']
     SepValue = request.POST['SepValue']
     SepValue2 = request.POST['SepValue2']

     OctMonth = request.POST['OctMonth']
     OctValue = request.POST['OctValue']
     OctValue2 = request.POST['OctValue2']

     NovMonth = request.POST['NovMonth']
     NovValue = request.POST['NovValue']
     NovValue2 = request.POST['NovValue2']

     DecMonth = request.POST['DecMonth']
     DecValue = request.POST['DecValue']
     DecValue2 = request.POST['DecValue2']

     userid = None
     if request.user.is_authenticated:
         userid = request.user.id
         print(userid)
         qs =graphinput.objects.filter(user_id=userid)

         if qs.filter(Month=JanMonth).exists():
              messages.info(request,'January month already exist')
              return redirect('index')

         elif qs.filter(Month=FebMonth).exists():
              messages.info(request,'February month already exist')
              return redirect('index')

         elif qs.filter(Month=MarMonth).exists():
              messages.info(request,'March month already exist')
              return redirect('index')

         elif qs.filter(Month=AprMonth).exists():
              messages.info(request,'April month already exist')
              return redirect('index')

         elif qs.filter(Month=MayMonth).exists():
              messages.info(request,'May month already exist')
              return redirect('index')

         elif qs.filter(Month=JunMonth).exists():
              messages.info(request,'June month already exist ')
              return redirect('index')

         elif qs.filter(Month=JulMonth).exists():
              messages.info(request,'July month already exist')
              return redirect('index')

         elif qs.filter(Month=AugMonth).exists():
              messages.info(request,'August month already exist')
              return redirect('index')

         elif qs.filter(Month=SepMonth).exists():
              messages.info(request,'September month already exist')
              return redirect('index')

         elif qs.filter(Month=OctMonth).exists():
              messages.info(request,'October month already exist')
              return redirect('index')

         elif qs.filter(Month=NovMonth).exists():
              messages.info(request,'November month already exist')
              return redirect('index')

         elif qs.filter(Month=DecMonth).exists():
              messages.info(request,'December month already exist')
              return redirect('index')

     
    

         else:
            graphplot = graphinput.objects.create(Month=JanMonth,Value1=JanValue,Value2=JanValue2,user=request.user)
            graphplot = graphinput.objects.create(Month=FebMonth,Value1=FebValue,Value2=FebValue2,user=request.user)
            graphplot = graphinput.objects.create(Month=MarMonth,Value1=MarValue,Value2=MarValue2,user=request.user)
            graphplot = graphinput.objects.create(Month=AprMonth,Value1=AprValue,Value2=AprValue2,user=request.user)
            graphplot = graphinput.objects.create(Month=MayMonth,Value1=MayValue,Value2=MayValue2,user=request.user)
            graphplot = graphinput.objects.create(Month=JunMonth,Value1=JunValue,Value2=JunValue2,user=request.user)
            graphplot = graphinput.objects.create(Month=JulMonth,Value1=JulValue,Value2=JulValue2,user=request.user)
            graphplot = graphinput.objects.create(Month=AugMonth,Value1=AugValue,Value2=AugValue2,user=request.user)
            graphplot = graphinput.objects.create(Month=SepMonth,Value1=SepValue,Value2=SepValue2,user=request.user)
            graphplot = graphinput.objects.create(Month=OctMonth,Value1=OctValue,Value2=OctValue2,user=request.user)
            graphplot = graphinput.objects.create(Month=NovMonth,Value1=NovValue,Value2=NovValue2,user=request.user)
            graphplot = graphinput.objects.create(Month=DecMonth,Value1=DecValue,Value2=DecValue2,user=request.user)
            graphplot.save();
            print('SUCCESFULLY ADDED')
            return redirect('chart_view')
     else:
            return redirect('index')

 else:
    return render(request, 'bp.html')

@login_required
def chart_view(request,month):
    user = request.user
    try:
        obj = graphinput.objects.get(user=user,Month=month)
    except graphinput.DoesNotExist:
        obj = None
    return render(request,"a.html",{"chart_obj":obj})
Jun 5, 2021 in Python by Bhavesh

edited Mar 4 26 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP