279

Half of my Flask routes requires a variable say, /<variable>/add or /<variable>/remove. How do I create links to those locations?

url_for() takes one argument for the function to route to but I can't add arguments?

6 Answers 6

411

It takes keyword arguments for the variables:

url_for('add', variable=foo)
url_for('remove', variable=foo)

The flask-server would have functions:

@app.route('/<variable>/add', methods=['GET', 'POST'])
def add(variable):

@app.route('/<variable>/remove', methods=['GET', 'POST'])
def remove(variable):
Sign up to request clarification or add additional context in comments.

3 Comments

But the problem is how 'foo' is beyond the scope if it's a variable from Python. Then, how do you solve it?
Just so that it is clearer, if you have @app.route("/<a>/<b>") and def function(a,b): ... as its function, then you should use url_for and specify its keyword arguments like this: url_for('function', a='somevalue', b='anothervalue')
This is also explained in the quickstart: flask.palletsprojects.com/en/2.2.x/quickstart/…
184

url_for in Flask is used for creating a URL to prevent the overhead of having to change URLs throughout an application (including in templates). Without url_for, if there is a change in the root URL of your app then you have to change it in every page where the link is present.

Syntax: url_for('name of the function of the route','parameters (if required)')

It can be used as:

@app.route('/index')
@app.route('/')
def index():
    return 'you are in the index page'

Now if you have a link the index page:you can use this:

<a href={{ url_for('index') }}>Index</a>

You can do a lot o stuff with it, for example:

# int has been used as a filter so that only integer can be passed in the url, otherwise it will give a 404 error
@app.route('/questions/<int:question_id>')
def find_question(question_id):  
    return ('you asked for question{0}'.format(question_id))

For the above we can use:

<a href = {{ url_for('find_question' ,question_id=1) }}>Question 1</a>

Like this you can simply pass the parameters!

4 Comments

I have a question, in the first example, the index method was passed as a string while in the second method, the find_question is being passed as variable. Why?
@AnandTyagi Is this what you mean? URL routing
@आनंद if variable you do: {{ url_for('find_question' ,question_id=question.id) }} and not {{ url_for('find_question' ,question_id={{question.id}}) }}
how to hash the int parameter?
52

Refer to the Flask API document for flask.url_for()

Other sample snippets of usage for linking js or css to your template are below.

<script src="{{ url_for('static', filename='jquery.min.js') }}"></script>

<link rel=stylesheet type=text/css href="{{ url_for('static', filename='style.css') }}">

Comments

23

Templates:

Pass function name and argument.

<a href="{{ url_for('get_blog_post',id = blog.id)}}">{{blog.title}}</a>

View,function

@app.route('/blog/post/<string:id>',methods=['GET'])
def get_blog_post(id):
    return id

Comments

1

You need to add function means that the page you want to render that function whould be added inside the url_for(function name). It will redirect to that function and the page will render accordingly.

Comments

1

If this can help, you can override the static folder when declaring your flask app.

app = Flask(__name__,
            static_folder='/path/to/static',
            template_folder='/path/to/templates')

Comments

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.