librelist archives

« back to archive

How to get search form value?

How to get search form value?

From:
Juan Christian
Date:
2014-10-09 @ 15:14
I'm doing a page with Flask-Bootstrap and I have a Search Form on the
navbar.

I want to be able to type something in there and press enter/ok, and go to
a URL like 'www.site.com/custom/what_I_typed', example: Search Form value =
'Oranges', I want it to redirect me to 'www.site.com/custom/Oranges', how
can I do that?

HTML: http://pastebin.com/EgA3agaS

Re: [flask] How to get search form value?

From:
Charles
Date:
2014-10-10 @ 01:13
On Thu, Oct 9, 2014 at 10:14 PM, Juan Christian
<juan0christian@gmail.com> wrote:
> I'm doing a page with Flask-Bootstrap and I have a Search Form on the
> navbar.
>
> I want to be able to type something in there and press enter/ok, and go to a
> URL like 'www.site.com/custom/what_I_typed', example: Search Form value =
> 'Oranges', I want it to redirect me to 'www.site.com/custom/Oranges', how
> can I do that?
>
> HTML: http://pastebin.com/EgA3agaS

Option 1: change the form action URL using javascript on submit
Option 2: redirect(url_for('...', query=request.args.get('input_name')))

Re: [flask] How to get search form value?

From:
Juan Christian
Date:
2014-10-12 @ 22:56
On Thu, Oct 9, 2014 at 10:13 PM, Charles <peacech@gmail.com> wrote:
>
> Option 1: change the form action URL using javascript on submit
> Option 2: redirect(url_for('...', query=request.args.get('input_name')))
>

Could give me a better explanation of the option 2? What's that '...'? My
form in the HTML is like this:

<form class="navbar-form navbar-right">
    <input type="text" class="form-control" placeholder="Search...">
</form>

Where do I need to put this redirect() method in my module?

Re: [flask] How to get search form value?

From:
Juan Christian
Date:
2014-10-13 @ 01:32
So people, got it working with this:


HTML:
<form class="navbar-form navbar-right" action="/" method="post">
    <input type="text" class="form-control" name='id64'
placeholder="Search...">
</form>

Code:
@app.route('/', methods=['POST'])
def search():
  id64 = request.form['id64']
  return redirect('/profiles/' + id64)

Re: [flask] How to get search form value?

From:
Scott Werner
Date:
2014-10-09 @ 16:10
Here is a general example. You would have to define routes for all search 
terms you want to redirect to. There are other ways of doing this too, 
e.g. taking the search term and finding the url endpoint in app.url_map. I
am not sure of your end goal here, but a basic search functionality will 
search the database and redirect to that resource url if found.

Can test with:
curl -include --form search=Oranges 127.0.0.1:5000/custom
curl -include --form search=oranges 127.0.0.1:5000/custom
curl -include --form search=Orange 127.0.0.1:5000/custom

############### example.py ##############
from flask import Flask, request, redirect, url_for
from werkzeug.routing import BuildError


app = Flask(__name__)
app.config['DEBUG'] = True
app.config['TESTING'] = True


@app.route('/custom', methods=['POST'])
def custom():
        form_data = request.form

        # change key to match form input name
        # <input name="search" type="text" ...>
        term = request.form['search']

        try:
                # term needs to match method name below
                # if term not found, catch exception
                url = url_for(term.lower().strip())
                return redirect(url)
        except BuildError:
                return 'Page not found!'


@app.route('/custom/Oranges', methods=['GET'])
def oranges():
        return 'Oranges Page'


@app.route('/custom/Apples', methods=['GET'])
def apples():
        return 'Apples Page'


if __name__ == '__main__':
    app.run()


Thanks,
Scott Werner

From: flask@librelist.com [mailto:flask@librelist.com] On Behalf Of Juan Christian
Sent: Thursday, October 09, 2014 11:15 AM
To: flask@librelist.com
Subject: [flask] How to get search form value?

I'm doing a page with Flask-Bootstrap and I have a Search Form on the navbar.

I want to be able to type something in there and press enter/ok, and go to
a URL like 
'www.site.com/custom/what_I_typed<http://www.site.com/custom/what_I_typed>',
example: Search Form value = 'Oranges', I want it to redirect me to 
'www.site.com/custom/Oranges<http://www.site.com/custom/Oranges>', how can
I do that?

HTML: http://pastebin.com/EgA3agaS

Re: [flask] How to get search form value?

From:
Juan Christian
Date:
2014-10-09 @ 16:34
On Thu, Oct 9, 2014 at 1:10 PM, Scott Werner <scott.werner@autodesk.com>
 wrote:

> Here is a general example. You would have to define routes for all search
> terms you want to redirect to. There are other ways of doing this too, e.g.
> taking the search term and finding the url endpoint in app.url_map. I am
> not sure of your end goal here, but a basic search functionality will
> search the database and redirect to that resource url if found.
>


Thanks, but I don't think this is the approach I need. I'll clarify my
needs:

In this project I'll have a procedurally generated page for users. It's
like this site, 'http://backpack.tf/', they don't have a hard-coded page
for each user, they generate it in "real-time" when you visit the user page
and apply a default template in it, like here '
http://backpack.tf/profiles/76561198044933832' where the ID (in this case
76561198044933832) is the only thing that will change in the requests.

I only need to get the ID from my search form, and then redirect to my page
'/profiles/ID_GOES_HERE', my current code for it is:

@app.route('/profiles/<int:steamid64>')
def profile(steamid64):
  user = User(steamid64)
    <... code ...>
    return render_template('profile.html', user=user)

It's working, but I need to type the ID by hand in the browser, I need to
be able to call it when I click "enter" in the search form, and get the
steamid from the form and pass it as an arg to the profile function.

Re: [flask] How to get search form value?

From:
Scott Werner
Date:
2014-10-09 @ 17:48
Updated code. I recommend going through the Flask Mega Tutorial 
(http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world)
to learn how to use WTForms in flask.

############### example_post_method.py ##############
from flask import Flask, request, redirect, url_for

app = Flask(__name__)

@app.route('/profiles/<steamid64>')
def profile(steamid64):
    user = User(steamid64)
    return render_template('profile.html', user=user)

@app.route('/profiles', methods=['POST'])
def profile_search():
    # <input name="steamid64" type="text" ...>
    steamid64 = request.form['steamid64']
    return redirect(url_for('profile', steamid64=steamid64))

if __name__ == '__main__':
    app.run()

Thanks,
Scott Werner

From: flask@librelist.com [mailto:flask@librelist.com] On Behalf Of Juan Christian
Sent: Thursday, October 09, 2014 12:34 PM
To: flask@librelist.com
Subject: Re: [flask] How to get search form value?

On Thu, Oct 9, 2014 at 1:10 PM, Scott Werner 
<scott.werner@autodesk.com<mailto:scott.werner@autodesk.com>> wrote:
Here is a general example. You would have to define routes for all search 
terms you want to redirect to. There are other ways of doing this too, 
e.g. taking the search term and finding the url endpoint in app.url_map. I
am not sure of your end goal here, but a basic search functionality will 
search the database and redirect to that resource url if found.


Thanks, but I don't think this is the approach I need. I'll clarify my needs:
< div>
In this project I'll have a procedurally generated page for users. It's 
like this site, 'http://backpack.tf/', they don't have a hard-coded page 
for each user, they generate it in "real-time" when you visit the user 
page and apply a default template in it, like here 
'http://backpack.tf/profiles/76561198044933832' where the ID (in this case
76561198044933832) is the only thing that will change in the requests.

I only need to get the ID from my search form, and then redirect to my 
page '/profiles/ID_GOES_HERE', my current code for it is:

@app.route('/profiles/<int:steamid64>')
def profile(steamid64):
    user = User(steamid64)
    <... code ...>
    return render_template(&# 39;profile.html', user=user)

It's working, but I need to type the ID by hand in the browser, I need to 
be able to call it when I click "enter" in the search form, and get the 
steamid from the form and pass it as an arg to the profile function.