求你了,我该怎么过去;int()参数必须是字符串、类似字节的对象或实数,而不是'非eType'"



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

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

我想这就是HTML文件的样子。该文件包含两个表单(<form>(,它们将向python代码发送POST请求。

I ID和每个表单的隐藏输入,以便您可以在代码中识别它们中的每一个

<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" id="form1">
{% csrf_token %}
<label for="price in packs"><strong>Price in packs</strong></label>
<input type="hidden" name="form" value="form1">
<input type="text" id="price in packs" name="price1" placeholder="Enter number between 20 and 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" id="form2">
{% csrf_token %}
<label for="price in packs"><strong>Price in packs</strong></label>
<input type="hidden" name="form" value="form2">
<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>

这里的第一个问题是您有两个表单,这意味着您的请求只能包含price1price2,但不能同时包含这两个,所以您必须为此做好准备。

另一个问题是,可以在不提供任何价值的情况下提交表格。这意味着price1price2可以是None,这有时不是一个好主意。

最后,将price1price2的类型设置为";文本":

<input type="text" id="price in packs" name="price1" placeholder="Enter number between 20 and 500">
...
<input type="text" id="price in packs" name="price2" placeholder="Enter number between 20 and 500">

这意味着如果有人提交了";苹果;表单会接受它。您应该将类型设置为";数字";只接受数字。

作为一种改进,您还可以设置min="20"(最低价格(和max="500"(最高价格(,以减轻必须编写以下内容的压力:

if price_pack_box >= 20 and price_pack_box <= 500:
...
else:
...

更改

将您的python代码重新映射到此

if request.method == "POST":
if request.POST["form"] == "form1":
price_pack_box = int(request.POST["price1"])
food = Food.objects.get(pk=40)
elif request.POST["form"] == "form2":
price_pack_box = int(request.POST["price2"])
food = Food.objects.get(pk=41)
food_price = food.food_price
total_price1 = price_pack_box*food_price
print(total_price1)

兄弟,您的代码仍然需要大量重构,否则您将不断遇到错误

最新更新