Пожалуйста, как мне обойти ошибку "аргумент int() должен быть строкой, байтоподобным объектом или действительным числом, а не 'NoneType'" в моем коде?

<h1>This is food box page</h1><br><br>

`<image src= "{% static 'food_app/image1.jpeg' %}">`
<h2>{{food_box1.0}}</h2>
<h3>{{food_box1.1}}</h3>
<button type= "button"><strong>EAT ME!</strong></button><br><br>
<form action="{% url 'food_app:price' %}" method="POST">
{% csrf_token %}
<label for="price in packs"><strong>Price in packs</strong></label>
`<input type="text" id="price in packs" name="price1" placeholder="Enter number between 20 and 
enter code h 500">` 
<h1>{{new_price}}</h1>
<button type= "button"><strong>EAT ME!</strong></button>
</form>

      

<br> <br>

<image src= "{% static 'food_app/image2.jpeg' %}">
<h2>{{food_box2.0}}</h2>
<h3>{{food_box2.1}}</h3>
<button type= "button"><strong>EAT ME!</strong></button><br><br>
<form action="{% url 'food_app:price' %}" method="POST">
{% csrf_token %}
<label for="price in packs"><strong>Price in packs</strong></label>
`<input type="text" id="price in packs" name="price2" placeholder="Enter number between 20 and 
500">`
<h1>{{new_price2}}</h1>
<button type= "button"><strong>EAT ME!</strong></button>            
</form>


            

Python

if request.method == "POST":


price_pack_box = int(request.GET.get("price1"))
if price_pack_box >= 20 and price_pack_box <= 500:
food = Food.objects.get(pk=40)
food_price = food.food_price
total_price1 = price_pack_box*food_price
elif price_pack_box < 20:
messages.info(request, "number input too small!")
return redirect('food_app:foodbox')
elif price_pack_box > 500:
messages.info(request, "number input too large!")
return redirect('food_app:foodbox')
print(total_price1)

 `elif request.method == "POST":`
 `price_pack_box = int(request.POST.get("price2"))`
     `if price_pack_box >= 20 and price_pack_box <= 500:`
     `food = Food.objects.get(pk=41)`   
     `food_price = food.food_price`
      total_price2 = price_pack_box*food_price
      elif price_pack_box < 20:
      messages.info(request, "number input too small!")
      return redirect('food_app:foodbox')
      elif price_pack_box > 500:
      messages.info(request, "number input too large!")
      return redirect('food_app:foodbox')
      print(total_price2)

my_dict = {'new_price':total_price1,'new_price2':total_price2,'new_price3':total_price3} return render(request, 'food_app/price.html', context=my_dict)

Это мой первый раз, когда я размещаю вопрос, пожалуйста, не обращайте внимания на мои ошибки

Change

price_pack_box = int(request.GET.get("price1"))

To

price_pack_box = int(request.GET.get("price1", 0))

Обеспечивает значение по умолчанию 0 для price1 в случае, если оно не предоставлено в форме

Вернуться на верх