Home > Back-end >  Set default value input tag Django
Set default value input tag Django

Time:10-15

IN HTML : <div style="margin-top: 1vh;"> <input name="gen_down" type="number" /> <span >%</span> </div>

IN VIWES.py: gen_down = int(request.POST.get('gen_down', 0))

Run in Terminal: gen_down = int(request.POST.get('gen_down', 0))

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

CodePudding user response:

Can be as simple as:

gen_down = int(request.POST.get('gen_down') or 0)

CodePudding user response:

Its very simple, the error say that we cannot do int() to a int value which in our case is zero so the solution is to return zero but in string format. Like this :

gen_down=int(request.POST.get('gen_down', "0")
  • Related