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.
I feel like you're asking two questions here but I'll take a shot...
For the posting url you'd do this:
To handle static files I'd highly suggest using an asset manager like Flask-Assets, but to do it with vanilla flask you do:
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:You have to change your view to something like this:
Then your template code can look like this:
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 :-).