Django URL警告URL.W002 [英] Django url warning urls.W002
问题描述
如果这个问题看起来有点多余,请提前表示抱歉,但这确实让我感到困扰.
Sorry in advance if this question look a bit superfluous but is something that is really bothering me.
我有一组由Django通过以下网址定义的API.
I have a set of API's written in Django defined by the following urls.
# urls.py
import ...
urlpatterns = [
url(r"^api/v1/account", include(profile.urls))
]
和
# profile/urls.py
import ...
urlpatterns = [
url(r"^$", AccountAPI.as_view()),
url(r"^/login$", LoginAPI.as_view()),
url(r"^/logout$", LogoutAPI.as_view())
]
此配置应仅允许 个网址:
This configuration should allow only the urls:
/api/v1/account
/api/v1/account/login
/api/v1/account/logout
这是出于我的目的,但我一直在发出类似警告(我用此规则定义了多个API,警告列表大得多):
This work for my purpose but I keep having warnings like(I have several API defined with this rule and the warning list is way bigger):
?: (urls.W002) Your URL pattern '^/login$' has a regex beginning with a '/'. Remove this slash as it is unnecessary.
?: (urls.W002) Your URL pattern '^/logout$' has a regex beginning with a '/'. Remove this slash as it is unnecessary.
如果删除斜杠,服务器将不会验证我定义的网址.为此,我必须对网址的第一级使用斜杠:
If I remove the slash the server will not validate the urls I defined. For that I have to had a slash to the first level of the urls like:
# urls.py
import ...
urlpatterns = [
url(r"^api/v1/account/", include(profile.urls))
]
这会使帐户呼叫以斜线结尾,这是我不希望的.
And this will make the account call to end with a slash, something I don't want to.
我觉得我已经以最优雅的方式定义了url,以达到我的目的,并且我的日志中不断出现这种可悲的警告.
I feel that I have defined the urls the most elegant way I found to serve my purpose and I keep having this sad warnings in my logs.
我做错什么了吗?有没有一种正确的方法来定义url而不损害我为其选择的结构?还是有一种方法可以关闭此警告?
Am I doing something wrong? Is there a right way to define the url's without compromising the structure I choose for them? Or there's a way of turning this warnings off?
推荐答案
我写支票时,我错误地认为URL带有斜杠,例如/api/v1/account/
和/api/v1/account/login/
.
When I wrote the check, I incorrectly assumed urls with trailing slashes, like /api/v1/account/
and /api/v1/account/login/
.
如果不使用斜杠,则以 ^/
开头的包含的网址格式可能是正确的,并且 W002
检查会给出错误的肯定结果.
If you do not use trailing slashes, then starting an included url pattern with ^/
can be correct, and the W002
check gives a false positive.
从 Django 1.10.2 开始,该检查已禁用如果您的设置中有 APPEND_SLASH = False
.有关讨论,请参见票27238 .
As of Django 1.10.2, the check is disabled if you have APPEND_SLASH=False
in your settings. See ticket 27238 for the discussion.
这篇关于Django URL警告URL.W002的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!