ValueError: urls must start with a leading slash
Thuruv opened this issue ยท 25 comments
I know somewhere I made a mistake but downloading and running it plainly gave me this error. I can understand somewhere in the routing the error is but I can find it, Can you help?
Traceback (most recent call last): File "app.py", line 17, in <module> app.register_blueprint(charts) File "C:\Users\c_thv\AppData\Local\Continuum\Anaconda2\envs\coffeemug\lib\site-packages\flask\app.py", line 64, in wrapper_func return f(self, *args, **kwargs) File "C:\Users\c_thv\AppData\Local\Continuum\Anaconda2\envs\coffeemug\lib\site-packages\flask\app.py", line 951, in register_blueprint blueprint.register(self, options, first_registration) File "C:\Users\c_thv\AppData\Local\Continuum\Anaconda2\envs\coffeemug\lib\site-packages\flask\blueprints.py", line 151, in register endpoint='static') File "C:\Users\c_thv\AppData\Local\Continuum\Anaconda2\envs\coffeemug\lib\site-packages\flask\blueprints.py", line 76, in add_url_rule view_func, defaults=defaults, **options) File "C:\Users\c_thv\AppData\Local\Continuum\Anaconda2\envs\coffeemug\lib\site-packages\flask\app.py", line 64, in wrapper_func return f(self, *args, **kwargs) File "C:\Users\c_thv\AppData\Local\Continuum\Anaconda2\envs\coffeemug\lib\site-packages\flask\app.py", line 1043, in add_url_rule rule = self.url_rule_class(rule, methods=methods, **options) File "C:\Users\c_thv\AppData\Local\Continuum\Anaconda2\envs\coffeemug\lib\site-packages\werkzeug\routing.py", line 603, in __init__ raise ValueError('urls must start with a leading slash') ValueError: urls must start with a leading slash
It looks like there is an invalid url somewhere in your flask app. Are you using the existing example app, or importing it into your own app instance? If the latter, make sure all your url routes have a leading slash, e.g.:
@app.route('foo/bar/')
def foobar():
pass
would need to become:
@app.route('/foo/bar/')
def foobar():
pass
Just did a quick check and all the routes in the example app.py
, example endpoints.py
, and the core blueprint charts_builder.py
have the correct leading slash.
@Thuruv I'll close this one if you've been able to isolate the problem, otherwise let me know, thanks!
Hi @christabor ,
I run into the same error. It might be an error that arise from the platform, as I am using MS Windows.
I get the same traceback as @Thuruv and find that when I put in a print() before the error it is related to static files. I had a look at chart_builder.py line 50. Here you use static_url_path=STATIC_DIR. This might not trough an exception on UNIX based systems, but on Windows, the path starts with a letter (fx C:...). The static_url_path should probably just be static_url_path="/STATIC/" or something. When I correct like that, the error is gone.
Kr
@madslundgaard Thanks, I've re-opened it and will take a look!
@madslundgaard does this work for you? I don't have a windows VM to test with at the moment. You can git apply
this as a patch.
diff --git i/flask_jsondash/charts_builder.py w/flask_jsondash/charts_builder.py
index 46c04c4..cc5e4b9 100644
--- i/flask_jsondash/charts_builder.py
+++ w/flask_jsondash/charts_builder.py
@@ -34,9 +34,16 @@ from .schema import (
validate_raw_json, InvalidSchemaError,
)
+IS_WINDOWS = os.name == 'nt'
TEMPLATE_DIR = os.path.dirname(templates.__file__)
STATIC_DIR = os.path.dirname(static.__file__)
+# The __file__ attribute does not appear to correctly handle slashes
+# for Windows. See https://github.com/christabor/flask_jsondash/issues/128
+if IS_WINDOWS:
+ TEMPLATE_DIR = os.path.normpath(TEMPLATE_DIR)
+ STATIC_DIR = os.path.normpath(STATIC_DIR)
+
# Internally required libs that are also shared in `settings.py` for charts.
# These follow the same format as what is loaded in `get_active_assets`
# so that shared libraries are loaded in the same manner for simplicty
Hi @christabor,
I don't think you need to change how the 'DIR' variables are assigned, as this is related to the URL for static files only.
For your correction, the normpath will only change slashes, not the file drive which will still be C:... I think
Ok, I think I understand a bit more clearly. However, the STATIC_DIR
and TEMPLATE_DIR
still need to be dynamically generated. This is so that the package can be imported and registered as a blueprint correctly when being installed with our without a virtualenv, instead of just cloning and copying code into a project.
But the issue of the drive letter and colon being prepended still remains. A different solution would be to regex match and then replace the first bit, e.g. C:\path\to\virtualenv\flask_jsondash\static\
becomes \path\to\virtualenv\flask_jsondash\static\
. But does this even guarantee it will work? I am not sure if flask can resolve the file path with that.
Either way, it does feel a bit hackish, but there are no obvious workarounds that I can see.
Hi,
You don't need to change the two variables, as they don't make any runtime errors where they are used for DIR assignment, only when they are used in the URL:
charts = Blueprint(
'jsondash',
name,
template_folder=TEMPLATE_DIR,
static_url_path=STATIC_DIR, <== HERE
static_folder=STATIC_DIR,
That assignment could just be left out, then it should default to /STATIC/, or if it need be another URL path /some/URL/path/ could be specified.
Same problem here trying to run example app in windows 10
In line 149 of blueprints.py, self.static_url_path equals C:\Users\... ...\flask_jsondash\static
(with my username and full path). The problem is it starts with C
.
I've change that line to
state.add_url_rule('/static' + '/<path:fi
view_func=self.send_st
endpoint='static')
and now it works.
I had the same problem, but it is working on windows 10 with VMWARE. I installed this project in Ubuntu
this problem still exists. just updating
same too
+1 win10 python3.6...
+1 win10 python 2.7.8
Sorry folks, I'll try to put some time into this over the weekend.
same problem
Hello, I have tried all the possible approaches but still having the same issue.
ValueError: urls must start with a leading slash
I am not getting where exactly I need to fix this issue.
Hi, i tried some approaches as well, but indeed, the same error on windows 10 +python 3.9 here.
Someone got this ?
Hello, I'm facing the same error too. Did anyone come across a solution?
Does anyone have a solution for this yet?
The fix for us atleast was dgtlmoon/changedetection.io@7e0f0d0#diff-fff535675c4953d2d9411ee893a00a3abcd2b966b698123616479e6cd3985015
basically remove the full path from static_url_path
:)
Thank You so much, it works
I had the same error with my odoo 11 project, windows 10 and python 3.7.
There is a URL that doesn't start with a leading slash (/) in the routing map :
routing_map.add(werkzeug.routing.Rule(url, endpoint=endpoint, methods=routing['methods'], **kw))
Simply replace the line your odoo code by this line :
routing_map.add(werkzeug.routing.Rule('/'+url.lstrip('/'), endpoint=endpoint, methods=routing['methods'], **kw))
Hopefully it works !