简体   繁体   中英

Error Message: why i am getting "None" although i have add get() at views.py in Django ??? Can anyone help me

This is my Views.py file and in this file I am trying to get the value from 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')

HTML Photo HTML 照片 HTML 照片

[![i have attached the output][1]][1]  

[1]: https://i.stack.imgur.com/y4pYy.png

If I understand it correctly, you want to know why you get None when you use the get() method on a dictionary/request.

This is because the requested key does not exist and you did not specify any default value for the get() that should be used if the key does not exist. In that case None is returned.

This is also explained in detail in this post.

Because you don't have input value in your html. So all the values are None .

Change your html from

<tr>
   <td>Full Name: </td>
   <td name="full_name">Shiva Giri<td>
</tr>

To

    <tr>
       <td>Full Name: </td>
       <td><input type="text" name="full_name" value="Shiva Giri"><td>
    </tr>

And so on for all other remaining values.

request.POST.get() gets values from <input> and <select> tags not from table tags.

You can try with hidden inputs like this: <input type="hidden" id="table_num" name="table_num" value="DC 8">

You will need to do this for all the information you want to send into the backend. Do not forget to add id="" and name="" to all inputs/select tags

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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