Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django url warning urls.W002

Tags:

python

url

django

Sorry in advance if this question look a bit superfluous but is something that is really bothering me.

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))
]

and

# 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

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.

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.

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?

like image 959
joaonrb Avatar asked Jan 03 '17 18:01

joaonrb


People also ask

How can we handle urls in Django?

Django runs through each URL pattern, in order, and stops at the first one that matches the requested URL, matching against path_info . Once one of the URL patterns matches, Django imports and calls the given view, which is a Python function (or a class-based view).

What is re_path in Django?

re_path is an implementation of the 'old' way of handling urls, which was previously (version <2) done by url from django. conf. urls . See the paragraph in Django 2.0 release notes about this.

How do I change the default URL in Django?

Set up app folder's urls.py and html files In the same directory, you should have a file named views.py. We will create a function called index which is what makes the http request for our website to be loaded. Now, we've set it up such that http://127.0.0.1:8000/homepage will render the HTML template index.

When should one use the include () in Django?

The include tag allows you include a template inside the current template. This is useful when you have a block of content that are the same for many pages.


2 Answers

When I wrote the check, I incorrectly assumed urls with trailing slashes, like /api/v1/account/ and /api/v1/account/login/.

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.

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.

like image 120
Alasdair Avatar answered Sep 19 '22 04:09

Alasdair


You can turn the warning(s) off using SILENCED_SYSTEM_CHECKS option.

Example:

SILENCED_SYSTEM_CHECKS = ['urls.W002', 'security.W019']
like image 23
Alex Vyushkov Avatar answered Sep 20 '22 04:09

Alex Vyushkov