0

I want to search for the name of snake that's in a list and based on that snakes ID, show that snake on a new page. I've been stuck on this for a few hours now and tried searching for it with no luck. I'm very rusty on HTML, maybe Im doing something wrong there?

 <form method="POST" action="{{url_for('search', name=name)}}">
        <input type="text" name="name">
        <input type="submit" value="Sök efter orm">
 </form>
@app.route('/search/<string:name>', methods=["GET", "POST"])
def search(name):
    found_snake = next(snake for snake in snakes if snake.name == name)
    found_id = found_snake.id

    return render_template('show.html', snake=found_id)
  • Can you confirm that name in your search function actually returns the input text you entered in your browser? Where is the snakes object or list defined, that you are using in the found_snake line? – gittert Dec 03 '19 at 10:46

1 Answers1

0

Since you are posting your form, you should not expect name to be part of the URL when you receive that data. Instead, change your search route to not wait for it and simply extract name from request.form like so:

from flask import request

@app.route('/search', methods=["POST"])
def search():
    name = request.form.get('name')

    # the rest is the same
    found_snake = next(snake for snake in snakes if snake.name == name)
    found_id = found_snake.id
    return render_template('show.html', snake=found_id)
dmitrybelyakov
  • 3,709
  • 2
  • 22
  • 26