Home > OS >  Getting None instead of Value from HTML form (Django)
Getting None instead of Value from HTML form (Django)

Time:09-27

Here below my HTML-template

<form action="{% url 'test-data' %}" method="POST" name="test" enctype="multipart/form-data">
   {% csrf_token %}
   <h2>
      {{ result }}
   </h2>
   <div >
      <button type="submit" >Show</button>
   </div>
</form>

my View.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. So, how I can get a Value inside curly brackets? Thanks a lot for any help.

CodePudding user response:

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. 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 >
   <input type="hidden" name="result_field" value="{{ result }}" />
   <button type="submit" >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.

  • Related