1

I'm trying to include an external javascipt file by a static URL like:

<script type="text/javascript" src="{{ url_for('static/js', filename='test.js') }}"></script>

but get following error:

werkzeug.routing.BuildError: ('static/js', {'filename': 'test.js', 'lang': 'en'}, None)

I use python flask framework for back end programming. It works well if i expand the javascript in the html file.

ericMTR
  • 211
  • 3
  • 14
  • do you have a `static/js/test.js` in the root of your project? – Joran Beasley Sep 05 '15 at 00:16
  • i geuss. at least I can access the javascript file by `http://127.0.0.1:5000/static/js/test.js` – ericMTR Sep 05 '15 at 00:34
  • The first argument to `url_for` is the name of the endpoint. Flask provides one named `static`. Any path information, such as `js/` should be included in the `filename` argument. – dirn Sep 05 '15 at 02:02

1 Answers1

3

according to this answer https://stackoverflow.com/a/20647713/541038

you should do this

<script type="text/javascript" src="{{ url_for('static", filename='/js/test.js') }}"></script>

[edit] as an aside you can probably just do

<script type="text/javascript" src="/static/js/test.js"></script>

(which is how I reference my static files ... unless I know for sure I am going to move them offsite to the cloud (If I move the whole app its fine ... its only if i plan to serve my static resources from a different server))

Community
  • 1
  • 1
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
  • Well, I don't get the error any more but still I can't access to my very basic function. I also tried your second suggestion - same result. how do you then access to your functions? Maybe I do it the completely wrong way... – ericMTR Sep 05 '15 at 00:36
  • you should have a folder named `static` in the root of the project (right next to `templates` folder ... and in static you should have a folder `js` and in that folder you should have `test.js` ... also hit f12 to and click the network tab to see what url it is trying to log – Joran Beasley Sep 05 '15 at 02:47
  • also I think it maybe needs a leading slash in the second example ... (and maybe first ... Im not sure) anyway – Joran Beasley Sep 05 '15 at 02:48