0

I have a quiz app and there are lots of interactions between JavaScript in templates and django views. Normally, on decent enough internet connection and less traffic the code works as it is supposed to. But as soon as the internet is slow and traffic is high (ie. 20 or so people are taking a quiz at the same time) . Django views stop catching values sent from forms in template Here is an example: Template code

 <form action='{% url "QuestionsAndPapers:EvaluateTest" %}' method='post' id ='finishForm'>
    {%csrf_token%}
    <input type="text" name="timeTaken" value="" class='hidden' />
    <button type="submit" id='testsub' value="{{te_id}}" class="btn btn-default text-center" name="testSub" onclick="formClick()" >Submit Test</button>
</form>

Django View:

def evaluate_test(request):
user = request.user
me = Studs(user)
if 'testSub' in request.POST:
    # get values of test id and total test time
    try:
        test_id = request.POST['testSub']
        test_id = int(test_id)

Now the problem is majority of times the template successfully returns {{te_id}} variable but when traffic is high and internet is slow evaluate_test function can't catch the {{te_id}} variable and it throws an error

ValueError: invalid literal for int() with base 10:

The problem is this error is not reproducible.

Prashant Pandey
  • 221
  • 3
  • 16
  • What is `te_id` variable? Can you show where you define it and how do you pass it to template? – neverwalkaloner Feb 16 '18 at 07:03
  • te_id is primary key for a specific test that the student is taking. When he attempts the last question and clicks on submit button(the form above) then that testid is suppose to be send to evaluate_test function to evaluate how many marks the student scored. – Prashant Pandey Feb 16 '18 at 07:15

1 Answers1

1

replace your line, test_id = int(test_id) with test_id = int(float(test_id))

Why this ValueError ?

>>> int('5.0')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '5.0'
>>> int(float('5.0'))
5

See this stackoverflow post for more information.

JPG
  • 82,442
  • 19
  • 127
  • 206
  • thanks for the answer but the problem is in my value error there is no literal , ie. ValueError: invalid literal for int() with base 10: ' '. There is no value inside the ' ' part , hence the error. – Prashant Pandey Feb 16 '18 at 08:47