1

I would like to handle an app route url in my Flask project, but unfortunately I can't set it up, because of the HTML form which holds the string, that I need for the proper app route.

My goal is this:

@app.route('/search-<content-from-the-search-form>')

However actually I can't even make it work anyway, (I assume) because the ? that is added by the form. I tried to edit the name tag in the HTML, but it adds a ? and = to the searched string content and couldn't find out how could I remove them.

So now one url looks like this after clicking the Search button:

/domain.com/?search=content-from-textbox

And I would like something like this:

/domain.com/search-content-from-textbox

Possibly somebody could tell me what I'm doing wrong? I can't even figure out if it's HTML only related issue and it can be solved by just editing the form or it needs changes in my flask config file too?

HTML:

  <form>
          <div class="form-group">
            <input type="search" class="form-control text-center input-lg" id="inputSearch" name="search" placeholder="search">
          </div>
          <br>
          <div class="text-center"><button type="submit" class="btn btn-primary btn-lg">Search!</button></div>
        </form>
3
  • Using parameters like ?search= to send queries to the server it totally Ok. If you still want to do it with a path, you have two options: a) use JAvascript on the clients side or use a redirect, which of cause would require two HTTP requests. Commented Dec 3, 2015 at 13:04
  • @KlausD. I've tried @app.route('/?search=<content-from-the-search-form>') too, but it doesn't worked, therefore I would better prefer to avoid it. Commented Dec 3, 2015 at 13:40
  • 1
    The ? starts a query string. That's how browsers submit forms that use a GET request (the default). There is no way to accomplish what you want through HTML. You will need to JavaScript to suppress the form submission and redirect the browser to your desired URL. Commented Dec 3, 2015 at 15:12

2 Answers 2

1

If you want to pass a variable from html, in this case your form back to flask, All you need to do is the following

<form action="{{url_for('view_name', variable='search-content-from-textbox-or-some-other-dynamic-data')}}">
   <div class="form-group">
      <input type="search" class="form-control text-center input-lg" id="inputSearch" name="search" placeholder="search">
      </div>
      <br>
      <div class="text-center"><button type="submit" class="btn btn-primary btn-lg">Search!</button></div>
</form>

and in your flask app:

@app.route('/search-<variable>')
def view_name():
    # do stuff

As you can see <variable> corresponds to the variable you pass in {{url_for('view_name', variable='search-content-from-textbox-or-some-other-dynamic-data')}}" in your html code. so whatever variable name you use in your view route, it has to match the variable name that you pass in url_for.

I hope that helps

Sign up to request clarification or add additional context in comments.

Comments

0

I would add this javascript into html page with the form.

<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script>
$("#search-form").submit(function(event){
    var value = $('#inputSearch').val();
    $.ajax({
        url: "search-" + value,
        type: "POST",
        success: function(response){
                console.log(response);
        }       
    })
    event.preventDefault();
 });
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.