Top ad position

Getting Flask BadRequestKeyError

Started 2 years ago by M R Alam in Framework, Flask

I am trying to get value of apptext from the form in my template, but seems like the request.form doesn't have the 'apptext'.

Body

I am trying to get value of apptext from the form in my template, but seems like the request.form doesn't have the 'apptext'.

This is the part of Flask code that are relevant

@app.route('/', methods = ["GET", "POST"])
def search():
    # if request.method == "POST":
    app.logger.info(request.form)
    app_name = request.form['apptext']

This is the part of the template file

{% extends 'base.html' %} 
{% block header %}
<h1>{% block title %}Search Apps{% endblock %}</h1>
{% endblock %} 
{% block content %}
<form method="post">
  <p><label for="apptext">App text</label></p>
  <p><input type="text" id="apptext" name="apptext"></p>
  <p><input type="submit" value="Search"></p>
</form>
{% endblock %}

So the path '/' just gave me the error below now:

werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.
KeyError: 'apptext'

1 Replies

  • Replied 2 years ago

    Report

    As I can see you really don't have apptext in your HTML form. There's appt.

    So try to fix this line

    app_name = request.form['apptext']
    

    and enable request type check

    if request.method == "POST":
Bottom ad position