0

I am building a webservice so people can search into the database. Lets say I have users and companies. Each user and company can be found thought their id. So if you search myurl/users/<id> you get information of that user, on the other hand if you search company/ you get information of that company. For this I have created two simple input texts (one for users and another for companies) where people can type the <id>. My problem is that when I get the value from the input text I get this myrul/users?<id> and not myurl/users/id. I tried to hardcode the slash but then I get myrul/users/?<id>. So my question is how can I get input text as a url and not as a variable.

I am using flask so my html has jinja2 code like this:

<!-- USER id -->
    <form method='GET' action={{url_for('get_info_by_id', type_collection='user')}}>
        <input type="text" name="my_id"/><input type="submit" value="Go">                         
    </form>

<!-- COMPANY id-->
    <form method='GET' action={{url_for('get_info_by_id', type_collection='company')}}>
        <input type="text" name="my_id"/><input type="submit" value="Go">                      
    </form>

In my python script (flask)

@app.route('myurl/<type_collection>/<my_id>')
get_info_by_id(type_collection,my_id):
    # search into the database and return info about that id
3
  • 2
    If you want form variables to be placed within the URL rather than the query string or post body, you'll need to use JavaScript to capture the form submission event and navigate to the new URL. Commented May 6, 2016 at 15:15
  • @dirn is correct, however look at my answer if you are just trying to get your form to search the database and return your objects. Commented May 7, 2016 at 6:15
  • I will go through JavaScript, thank you. Commented May 9, 2016 at 10:37

2 Answers 2

1

As @dirn suggested in the commentary, I made it through JavaScript, here is the code if someone else is also interested:

HTML:

<!-- USER id -->
<form method='GET' class="search" id="user" action="">
    <input type="text" name="my_id"/><input type="submit" value="Go">
</form>

<!-- COMPANY id-->
<form method='GET' class="search" id="company" action="">
    <input type="text" name="my_id"/><input type="submit" value="Go">
</form>

JS:

$(".search").submit(function( event ){
    event.preventDefault();
    var my_id = $(this).find(":input").val();
    url = 'myurl/'+ $(this).attr("id") + '/' + my_id;
    window.location.href = url;     
});

python (flask)

@app.route('myurl/<type_collection>/<my_id>')
get_info_by_id(type_collection,my_id):
    # search into the database and return info about that id
Sign up to request clarification or add additional context in comments.

Comments

0

Is there a reason you cannot use the variable, or are you just trying to get it into a URL so that you can do your search? I went out on a limb and assumed you just want the form and database search to work, so try the following out.

Adjust your route like so:

@app.route('myurl/<type_collection>/')
def findAllTheThings():
    if not request.form['my_id']: # Just check if a specific entity is chosen
        return render_template('YourTemplateHere') # If no entity, then render form 
    entity_id = request.form['my_id']
    get_info_by_id(type_collection, entity_id):
    # search into the database and return info about that id

Now adjust the template as follows:

<!-- USER id -->
<form method='GET' action={{url_for('findAllTheThings', type_collection='user')}}>
    <input type="text" name="my_id"/><input type="submit" value="Go">                         
</form>

<!-- COMPANY id-->
<form method='GET' action={{url_for('findAllTheThings', type_collection='company')}}>
    <input type="text" name="my_id"/><input type="submit" value="Go">                      
</form>

Now, if no entity has been selected you'll just render the form. You can throw in a flash to let them know they need to select a specific ID, or just let them figure it out. If an entity has been selected, you will call the fucntion correctly.

1 Comment

thank you but I do need to make it as an url and not as a variable

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.