Asma
Asma

Reputation: 137

Flask - Send POST request to specific section in the same page

I'm building a simple web site using flask, witch contain one page html index.html, and sections : search, about, contact. This sections are not displayed until the user enter to them

I want to send a POST request from index.html/#search to the same section : index.html/#search.

I tried to send it like that :

@app.route('/search', methods=['POST'])
def Search():
    _text = request.form['text']
    return render_template('index.html/#search', result=_text)

but that didn't work, and I get this error :

jinja2.exceptions.TemplateNotFound: index.html/#search

I tried to pass search as parameter :

@app.route('/search', methods=['POST'])
def Search():
    _text = request.form['text']
    return render_template('index.html', id="search", result=_text)

but when I run it didn't works because it takes me to http://127.0.0.1:5000/search (witch displayed the index.html page) while I want to go to http://127.0.0.1:5000/#search.

This is my index.html page:

<article id="search">
   <h2 class="major">search</h2>
   <form method="post" action="Search">
       <label for="text">Text</label>
       <input type="text" name="text" id="text" placeholder="write any thing in your mind :p" required/>
       <input name="search" type="submit" value="See Polarity" class="special" />
       <input type="reset" value="Reset" />
   </form>

   <!-- result search -->
   {% if result : %}
       <p>{{ result }}</p>
   {% endif %}
</article>

Upvotes: 0

Views: 2180

Answers (1)

Iain Dwyer
Iain Dwyer

Reputation: 184

The # represents an anchor on an existing page, not a new page.

Similarly, your syntax of :

@app.route('/search', methods=['POST'])

Would create a new route to http://127.0.0.1:5000/search . If you want it to render index.html and then jump to the search anchor you should have it render index.html as you've done it, include the _anchor attribute. It would look like:

@app.route('/', methods=['GET','POST'])
def Search():
    if request.method == 'GET'
        return render_template('index.html')
    else:      
        _text = request.form['text']
        return render_template('index.html', _anchor="search", result=_text)

Upvotes: 1

Related Questions