简体   繁体   English

对于由 javascript 设置的字段值,Django request.POST.get() 为空

[英]Django request.POST.get() is empty for field values set by javascript

I have a html form where user provides length, width and height and there is onkeyup event linked to a javascript function which in return provides volume in a disabled textbox.我有一个 html 表单,其中用户提供长度、宽度和高度,并且有一个链接到 javascript 函数的 onkeyup 事件,该函数反过来在禁用的文本框中提供音量。 Now, the values are shown in the textbox and they do change dynamically but when I am submitting the form and accessing it in the views using POST method, I am only getting values for input fields which are filled by user and the disabled textbox returns None .现在,这些值显示在文本框中,并且它们确实会动态更改,但是当我提交表单并使用 POST 方法在视图中访问它时,我只获取用户填写的输入字段的值,禁用的文本框返回None .

I have to store that volume in session and pass it to next view on requested but since the request.POST.get() is None, I am not able to retrieve it.我必须在会话中存储该卷并在请求时将其传递给下一个视图,但由于request.POST.get()为 None,我无法检索它。

Note : I am just using volume calculation as an example to explain the issue.注意:我只是以体积计算为例来解释这个问题。

# html

<head>
   <script>
       calcVolume(){
         var Length = Number(document.getElementsByName('length')[0].value);
         var Width = Number(document.getElementsByName('width')[0].value);
         var Height = Number(document.getElementsByName('height')[0].value);
         if (Length != '' && Width != '' && Height != '') {
             var Volume = Length * Width * Height
             document.getElementsByName('volume')[0].value = Volume;
         }
       }
   <script>
</head>    

<form method="POST" action="">
   {csrf_token}
   <table>
     <tr>
     <td><input name="length" onkeyup="calcVolume();"></td>
     <td><input name="width" onkeyup="calcVolume();"></td>
     <td><input name="height" onkeyup="calcVolume();"></td>
     <td><input name="volume" value="0" disabled></td>
     <td><button type="submit">Next</button></td>
     </tr>
   </table>
</form>



# views.py

def vol(request):
   if method.request == "POST":
     request.session['l'] = request.POST['length']
     request.session['w'] = request.POST['width']
     request.session['h'] = request.POST['height']
     request.session['volume'] = request.POST.get('volume')
     print(request.session['volume']) # here the volume is None
     return redirect('/whatever_next_page')
   else:
     return render(request,'calculate_volume.html')

A disabled formfield is not sent to the server: disabled表单域不会发送到服务器:

The difference between disabled and readonly is that read-only controls can still function and are still focusable, whereas disabled controls can not receive focus and are not submitted with the form and generally do not function as controls until they are enabled. disabled 和 readonly 之间的区别在于,只读控件仍然可以运行并且仍然是可聚焦的,而禁用的控件不能获得焦点并且不随表单提交,并且通常在启用之前不能用作控件。

https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/disabled https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/disabled

By using readonly instead of disabled your code will probably start working.通过使用readonly而不是disabled您的代码可能会开始工作。

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

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