简体   繁体   English

从 HTML 表单(Django)获取 None 而不是 Value

[英]Getting None instead of Value from HTML form (Django)

Here below my HTML-template在我的 HTML 模板下面

<form action="{% url 'test-data' %}" method="POST" name="test" enctype="multipart/form-data">
   {% csrf_token %}
   <h2>
      {{ result }}
   </h2>
   <div class="d-flex justify-content-center">
      <button type="submit" class="btn btn-primary">Show</button>
   </div>
</form>

my View.py我的视图.py

def show_result(request):
    if request.method == "POST":
        result = request.POST.get('test')
        return HttpResponse(result)

By pressing the button 'Show' I got None instead of {{ result }} value.通过按下“显示”按钮,我得到了 None 而不是 {{ result }} 值。 So, how I can get a Value inside curly brackets?那么,如何在大括号内获得一个值? Thanks a lot for any help.非常感谢您的帮助。

In order to submit the data, they need to be values of form elements like input, textarea, select options... You can choose the right type for the input field.为了提交数据,它们需要是表单元素的值,例如 input、textarea、select options... 您可以为输入字段选择正确的类型。 You can make use of the hidden field type to submit data that will not be displayed to the client... You probably do not need to use the heading inside the form.您可以使用隐藏字段类型来提交不会显示给客户端的数据...您可能不需要在表单中使用标题。

<h2>{{ result }}</h2>
<form action="{% url 'test-data' %}" method="POST" name="test" enctype="multipart/form-data">
{% csrf_token %}
<div class="d-flex justify-content-center">
   <input type="hidden" name="result_field" value="{{ result }}" />
   <button type="submit" class="btn btn-primary">Show</button>
</div>
</form>

On the backend, you can retrieve the data as follow:在后端,您可以按如下方式检索数据:

result = request.POST.get('result_field')

I hope that works for you.我希望这对你有用。

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

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