url_for is a function present in web frameworks like Rails and Flask, used to get the URL for a given route.
Questions tagged [url-for]
184 questions
71
votes
1 answer
Where do I define the domain to be used by url_for() in Flask?
When I call url_for('index') it will generate '/' but there are times where I'd like it to generate 'domain.tld/' instead. I can't find in the documentation where I would specify this. Do I just need to do 'domain.tld/%s' % url_for('index')?
![](../../users/profiles/1389451.webp)
Mark
- 1,367
- 4
- 12
- 17
42
votes
4 answers
rails get app root/base url
In my app I have a few APIs that under api domain. Now in one of the API I want to generate a url that pointing to the main domain, say
test.com/blabla...
I tried to use url_for but seems the default root_url or request.host is in api domain.…
![](../../users/profiles/1174306.webp)
Bruce Xinda Lin
- 2,470
- 6
- 24
- 37
42
votes
1 answer
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///')
def get_post(year,month,title):
# My code
To display the last 10 entries, I have following…
![](../../users/profiles/679047.webp)
codecool
- 5,148
- 4
- 24
- 35
42
votes
8 answers
Flask url_for URLs in Javascript
What is the recommended way to create dynamic URLs in Javascript files when using flask? In the jinja2 templates and within the python views url_for is used, what is the recommended way to do this in .js files? Since they are not interpreted by the…
![](../../users/profiles/224732.webp)
Erik Rothoff
- 4,088
- 8
- 39
- 54
38
votes
4 answers
Query parameters with url_for?
url_for([:edit, @post])
is working and generating /comments/123/edit. Now I need to add a query parameter, so that instead of
/comments/123/edit
it is
/comments/123/edit?qp=asdf
I tried url_for([:edit, @post], :qp => "asdf") but no go.
![](../../users/profiles/377920.webp)
randomguy
- 11,352
- 14
- 65
- 94
18
votes
7 answers
Build error with variables and url_for in Flask
Have found one or two people on the interwebs with similar problems, but haven't seen a solution posted anywhere. I'm getting a build error from the code/template below, but can't figure out where the issue is or why it's occurring. It appears that…
![](../../users/profiles/1171947.webp)
Rob
- 181
- 1
- 1
- 4
18
votes
1 answer
How do i link to images not in Static folder in flask
In flask, how do I serve images that are not in the static folder?
I currently save the user uploaded photos on a directory that is outside the flask folder (On openshift, image currently saving in the data folder under app-root/data and the flask…
![](../../users/profiles/786326.webp)
Shankar ARUL
- 9,495
- 8
- 59
- 62
17
votes
2 answers
how to use Flask Jinja2 url_for with multiple parameters
I have a problem while using jinja2 url_for() function.
I have a route like this:
@app.route('/article///', methods=['GET'])
def article_page(article_id, url_title):
article = Article.query.get(article_id)
if…
![](../../users/profiles/1392295.webp)
Tiny Lin
- 208
- 1
- 2
- 7
16
votes
5 answers
Rails: generate a full URL in an ActionMailer view
I'm using ActionMailer to send a sign up confirmation email. The email needs to contain a link back to the site to verify the user, but I can't persuade Rails to generate a full URL (including the domain etc).
I'm using:
<%= url_for :controller =>…
![](../../users/profiles/23008.webp)
Mike Sutton
- 4,138
- 4
- 25
- 41
15
votes
3 answers
How to load a javascript or css file into a BottlePy template?
I am trying to return a html template with BottlePy. And this works fine. But if I insert a javascript file like this in my tpl-file:
I get an 404 error.
(Failed to load…
![](../../users/profiles/438743.webp)
eltorrero
- 307
- 1
- 2
- 6
15
votes
4 answers
Rails url_for and namespaced models
In Rails, is it possible to namespace models in modules and still get correct behavior from url_for?
For instance, here, url_for works as expected:
# app/models/user.rb
class User < ActiveRecord::Base
end
# config/routes.rb
resources :users
#…
![](../../users/profiles/92049.webp)
sebastian
- 1,168
- 1
- 12
- 20
12
votes
1 answer
Jinja variables within the Flask url_for function
I have tried multiple different things, but cannot get the URL to appear as /item like I want it to. I know that everything else is functioning correctly because if I simply replace {{item}} with a string, it works fine. Am I missing something…
![](../../users/profiles/2390195.webp)
Excuse.my.stupidity
- 241
- 1
- 2
- 7
11
votes
1 answer
Flask: Static files in subdirectories
In my flask template file I include a css file (I ommited the boilerplate) like this:
url_for('static', filename='css/bootstrap.css')
This renders to /static/css/bootstrap.css which means (because of leading slash) it is interpreted as…
![](../../users/profiles/488265.webp)
Jonas Gröger
- 1,347
- 2
- 19
- 32
10
votes
1 answer
Flask: where to put static javascript files in templates
I'm developing a flask app with the following folder structure:
|-->flask_app.py
|-->static
|-->css
|-->bootstrap.min.css
|-->styles.css
|-->js
|-->jquery-3.1.1.min.js
…
![](../../users/profiles/6611672.webp)
Johnny Metz
- 3,186
- 6
- 40
- 97
7
votes
1 answer
Jinja ignores HTML comments
Not a question per sae but an observation others may find helpful.
I have had issues with
'werkzeug.routing.BuildError: Could not build url for endpoint ... '
errors when using Flask to build a website.
As part of my development I had created a menu…
![](../../users/profiles/8899437.webp)
mhwalmsley
- 71
- 1
- 3