Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating link to an url of Flask app in jinja2 template

In my Flask app, I have a view which displays a post

@post_blueprint.route('/post/<int:year>/<int:month>/<title>') def get_post(year,month,title):     # My code 

To display the last 10 entries, I have following view:

@post_blueprint.route('/posts/') def get_all_posts():     # My code     return render_template('p.html',posts=posts) 

Now when I display the last 10 posts, I want to convert the title of a post into a hyperlink. Currently I have to do the following in my jinja template to achieve this:

<a href="/post/{{year}}/{{month}}/{{title}}">{{title}}</a> 

Is there any way to avoid hard coding the url?

Like url_for function which is used to create Flask urls like this:

url_for('view_name',**arguments) 

I have tried searching for one but I,m not able to find it.

like image 904
codecool Avatar asked Jun 20 '12 17:06

codecool


People also ask

How do I give a Flask a URL?

The url_for() function is used to build a URL to the specific function dynamically. The first argument is the name of the specified function, and then we can pass any number of keyword argument corresponding to the variable part of the URL.

How do I link a template in Flask?

html template file in a directory called templates inside your flask_app directory. Flask looks for templates in the templates directory, which is called templates , so the name is important. Make sure you're inside the flask_app directory and run the following command to create the templates directory: mkdir templates.

Does Jinja2 have Flask?

Flask comes packaged with Jinja2, and hence we just need to install Flask. For this series, I recommend using the development version of Flask, which includes much more stable command line support among many other features and improvements to Flask in general.


1 Answers

I feel like you're asking two questions here but I'll take a shot...

For the posting url you'd do this:

<a href="{{ url_for('post_blueprint.get_post', year=year, month=month, title=title)}}">     {{ title }} </a> 

To handle static files I'd highly suggest using an asset manager like Flask-Assets, but to do it with vanilla flask you do:

{{ url_for('static', filename='[filenameofstaticfile]') }} 

If you'd like more information I highly suggest you read. http://flask.pocoo.org/docs/quickstart/#static-files and http://flask.pocoo.org/docs/quickstart/#url-building

Edit for using kwargs:

Just thought I'd be more thorough...

If you'd like to use url_for like this:

{{ url_for('post_blueprint.get_post', **post) }} 

You have to change your view to something like this:

@post_blueprint.route('/posts/') def get_all_posts():     models = database_call_of_some_kind # This is assuming you use some kind of model     posts = []     for model in models:         posts.append(dict(year=model.year, month=model.month, title=model.title))     return render_template('p.html', posts=posts) 

Then your template code can look like this:

{% for post in posts %}     <a href="{{ url_for('post_blueprint.get_post', **post) }}">         {{ post['title'] }}     </a> {% endfor %} 

At this point I would actually create a method on the model so you don't have to turn it into a dict, but going that far is up to you :-).

like image 197
ravenac95 Avatar answered Sep 18 '22 18:09

ravenac95