繁体   English   中英

请问,我如何克服“int() 参数必须是字符串、类似字节的 object 或实数,而不是“NoneType””代码中的错误?

[英]please, how do I get over "int() argument must be a string, a byte-like object or a real number, not a 'NoneType'" error in my code?

<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)

这是我第一次发布问题,请不要介意我的错误

改变

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

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

这提供了 0 到price1的默认值,以防它未在表单中提供

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM