![](/img/trans.png)
[英]can anyone help me, why I am getting food is not defined error ? I have attached the structure of Django module
[英]Error Message: why i am getting "None" although i have add get() at views.py in Django ??? Can anyone help me
這是我的 Views.py 文件,在這個文件中我試圖從 HTML 獲取值
from django.shortcuts import render
from .models import kitchenData
from django.http import HttpResponse
def kitchen(request):
if request.method == 'POST':
table_num = request.POST.get('table_num')
full_name = request.POST.get('full_name')
qty = request.POST.get('qty')
drink_name = request.POST.get('drinks_name')
drink_qty = request.POST.get('drinks_qty')
message = request.POST.get('message')
price = request.POST.get('price')
status = request.POST.get('status')
print(table_num,full_name,qty,drink_name,drink_qty,message,price,status)
return render(request, 'kitchen/kitchen_order.html')
else:
return render(request, 'kitchen/kitchen_order.html')
[![i have attached the output][1]][1]
[1]: https://i.stack.imgur.com/y4pYy.png
如果我理解正確,您想知道在字典/請求上使用get()
方法時為什么會得到None
。
這是因為請求的鍵不存在,並且您沒有為get()
指定任何默認值,如果該鍵不存在,則應使用該默認值。 在這種情況下,返回None
。
因為您的 html 中沒有input
值。 所以所有的values
都是None
。
將您的html
從
<tr>
<td>Full Name: </td>
<td name="full_name">Shiva Giri<td>
</tr>
至
<tr>
<td>Full Name: </td>
<td><input type="text" name="full_name" value="Shiva Giri"><td>
</tr>
對於所有其他remaining
值,依此類推。
request.POST.get()
從<input>
和<select>
標簽獲取值,而不是從 table 標簽。
您可以嘗試使用這樣的隱藏輸入: <input type="hidden" id="table_num" name="table_num" value="DC 8">
您需要對要發送到后端的所有信息執行此操作。 不要忘記將id=""
和name=""
添加到所有輸入/選擇標簽
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.