4

I want to make a redirection to a URL and pass variable 'file' to it , can someone please help . Here is the view :

@app.route('/api/uploads/<string:file>/', methods=['GET','COPY']) 
def download(file):
   sub = db.session.query(func.max(Content.Hits).label('max_hit')).subquery()
  contenu = db.session.query(Content).join(sub, sub.c.max_hit == Content.Hits).all()
name1 = contenu[0].name

if name1 == file:
   return redirect('http://192.168.198.134:5000/api/uploads/<string:file>', file)

else:   
    return send_from_directory(UPLOAD_FOLDER, file) 

1 Answer 1

8

This is what the url_for() function is for:

from Flask import url_for

redirect(url_for(download, file=file))

url_for() takes the endpoint name of your view (by default the name of your function, here download), and additional keyword arguments to provide values for the parameters.

Also see the URL Building section in the Quickstart documentation.

Sign up to request clarification or add additional context in comments.

4 Comments

sorry but the URL points to another flask server running on another machine . i think url_for can be used if i redirect to a page on the same server
@Rzozi: then use url_for() to build the path, and then post-process that URL to set the servername.
@MartijnPerters : Can i do it like this : return redirect('192.168.198.134:5000 %s' % url_for(download,file=file)
You'll have to be careful to escape the path element properly. url_for() handles this for you already.

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.