0

I want to pass variables from a site to another. This is no problem, as there are many ways to do it. I'm struggling though, in how I can 'hide' these variables in the URL, and yet be able to get the values. Ex.: If I use 'request.args.get':

@page.route('/users', methods=['GET', 'POST'])
def users():
    user = request.args.get('user')
    return render_template('users.html', user=user)

When I click in the link, the URL generated is: http://localhost:5000/users?user=john

My goal is to access the 'users' page, in the 'John' section, but what the user will see in the URL path is only http://localhost:5000/users

Dumb admin
  • 49
  • 2
  • 9
  • 7
    just use post instead of get? – Florian H Jan 10 '18 at 10:18
  • May I ask why you want to hide it? – gonczor Jan 10 '18 at 11:23
  • 1
    @gonczor: Because the other page has other 'anchors' for other users that I Hide and Show with JQuery, dynamically. And when, for example, the user John us hidden and user 'Blabla' is shown, the URL keeps the 'JOHN' on it. It's no big deal, it just annoys me. – Dumb admin Jan 10 '18 at 13:20

3 Answers3

3

If you'd only want to hide the variable name then you could use converters to create a route like 'users/<str:username>'. Your url would be http://localhost:5000/users/john.

Your can find the documentation here: http://exploreflask.com/en/latest/views.html#built-in-converters

Note that hiding the variables completely would mean, that your users would lose the ability to bookmark the page they are on. Additionaly if they bookmark /users anyways, you would have to catch the case that your variable is not sent or run into errors.

Tekay37
  • 467
  • 5
  • 18
  • Thanks, although I have attempted this already and is not quite what I am looking for. The anchor's and users are dynamically generated, therefore bookmarks aren't a concern. – Dumb admin Jan 10 '18 at 13:48
3

I was able to achieve my goal using:

window.history.pushState({"html":response.html,"pageTitle":response.pageTitle},"", "/users/");

I'm no Web Dev'er, just a Python/Flask enthusiast and know that 'window.history.pushState()' is meant for other purposes. I'm also aware that it a HTML5 Feature and not all browsers are compatible. But hey, it did the trick ;) .

Unless someone point out reasons I shouldn't be using this approach, this is my solution.

Thanks all for your time

Dumb admin
  • 49
  • 2
  • 9
1

Post method can hide data and variables from URL. So you need to integrate it in your project. Here is an example.

app.py:

from flask import Flask, render_template, request

app = Flask(__name__)

@app.route('/users', methods=['GET', 'POST'])
def show_users():
    if request.method == 'POST':
        username = request.form.get("username", None)
        return render_template('post_example.html', username = username)
    else:
        return render_template('post_example.html')

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

post_example.html:

<html>
  <head></head>
  <body>
    {% if username %}
      Passed username: {{ username }}
    {% endif %}
    <form action="/users" method="post">
      Username: <input type="text" name="username">
      <input type="submit" name="submit" value="Submit">
    </form>
  </body>
</html>

Output:

post example in Flask

You can check the HTTP methods in Flask official documentation here

arshovon
  • 13,270
  • 9
  • 51
  • 69
  • 1
    This is what I need. Although my App is a little more complex. The links that forward to the 'users' page are inside a tree-menu. And there could be hundreds of users that are always dynamically generated in the tree. I will try to implement it in my code. Thanks! – Dumb admin Jan 10 '18 at 13:43