fid
fid

Reputation: 72

Redirect to same page but add dynamic route

I'm building a webapp where you can add Player-Profiles into a table to compare these. I've posted all changes to same URL, now I found out that the changes are seen by all users.

I considered how to fix this: Instead of url .../stats I tried to individualize url for every Profile added to the table like .../stats?id=237 (for one profile) .../stats?id=237id=335 (for two profiles).

Method for the stats:

@app.route('/stats/', methods=['POST', 'GET'])
def pstats():
    ...
    ...
    urla= [237, 335]
    return redirect(url_for('pstats', data = urla ))
  1. route: .../stats enter image description here
  2. route: .../stats?id=237 enter image description here
  3. route: .../stats?id=237id=335 enter image description here

How can I write the route of pstats dynamically that it can look like 1. route or 2. route?

Upvotes: 0

Views: 47

Answers (1)

Simeon Nedkov
Simeon Nedkov

Reputation: 1094

Use request.args to get the query parameters that are passed to your route.

@app.route('/stats/', methods=['POST', 'GET'])
def pstats():
    id = request.args['id']

Pass id to url_for to generate a URL that contains query parameters.

url_for(url_for('pstats', id=id))

Upvotes: 1

Related Questions