Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ValueError: urls must start with a leading slash #128

Open
Thuruv opened this issue Jun 21, 2017 · 25 comments
Open

ValueError: urls must start with a leading slash #128

Thuruv opened this issue Jun 21, 2017 · 25 comments

Comments

@Thuruv
Copy link

Thuruv commented Jun 21, 2017

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

@christabor
Copy link
Owner

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

@christabor
Copy link
Owner

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.

@christabor
Copy link
Owner

@Thuruv I'll close this one if you've been able to isolate the problem, otherwise let me know, thanks!

@madslundgaard
Copy link

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

@christabor christabor reopened this Oct 2, 2017
@christabor
Copy link
Owner

christabor commented Oct 2, 2017

@madslundgaard Thanks, I've re-opened it and will take a look!

@christabor
Copy link
Owner

@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

@madslundgaard
Copy link

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.

@madslundgaard
Copy link

For your correction, the normpath will only change slashes, not the file drive which will still be C:... I think

@christabor
Copy link
Owner

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.

@madslundgaard
Copy link

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.

@Susensio
Copy link

Same problem here trying to run example app in windows 10

@Susensio
Copy link

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.

@ghost
Copy link

ghost commented Mar 21, 2018

I had the same problem, but it is working on windows 10 with VMWARE. I installed this project in Ubuntu

@maverik-akagami
Copy link

this problem still exists. just updating

@quanpower
Copy link

same too

@randomgenerator2019
Copy link

randomgenerator2019 commented Dec 20, 2018

+1 win10 python3.6...

+1 win10 python 2.7.8

@christabor
Copy link
Owner

Sorry folks, I'll try to put some time into this over the weekend.

@nmaas87
Copy link

nmaas87 commented Feb 28, 2019

same problem

@ShaileshSarda
Copy link

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.

@Chuckpy
Copy link

Chuckpy commented Mar 19, 2021

Hi, i tried some approaches as well, but indeed, the same error on windows 10 +python 3.9 here.
Someone got this ?

@rajatsaxena0707
Copy link

Hello, I'm facing the same error too. Did anyone come across a solution?

@utsav-enact
Copy link

Does anyone have a solution for this yet?

@dgtlmoon
Copy link

The fix for us atleast was dgtlmoon/changedetection.io@7e0f0d0#diff-fff535675c4953d2d9411ee893a00a3abcd2b966b698123616479e6cd3985015

basically remove the full path from static_url_path :)

@Usman4862
Copy link

Thank You so much, it works

@OumeymaJouini
Copy link

OumeymaJouini commented Mar 25, 2023

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 !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests