Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why django urls end with a slash?

Tags:

python

django

Django official documentation and other tutorials on the web always use a trailing slash at the end of url. ex:

url(r'^accounts/login/', views.login)  # login view in turn calls login.html  # instead of  url(r'^accounts/login', views.login) 

Since accounts is the directory and login (login.html) is the file, shouldn't we be using second url? This will also make GET parameters look more structured:

accounts/login?name='abc'  # login is a file that is accepting parameters vs. accounts/login/?name='abc' # login directory (maybe index) is accepting parameters?? 
like image 868
The Wanderer Avatar asked Feb 13 '17 19:02

The Wanderer


People also ask

Why do some URLs have a slash at the end?

Conventionally, a trailing slash (/) at the end of a URL meant that the URL was a folder or directory. At the same time, a URL without a trailing slash at the end used to mean that the URL was a file. However, this isn't how many websites are structured today.

What happens if you skip trailing slash in Django?

If you're creting a RESTful API using Django, this can be a good solution when developers POST data directly to endpoint URL. When using APPEND_SLASH , if they accidently sent it without trailing slash, and your urlconf is WITH a trailing slash they would get an exception about data lose when redirecting POST requests.

Should URLs end in trailing slash?

If your site has a directory structure, it's more conventional to use a trailing slash with your directory URLs (for example, example.com/directory/ rather than example.com/directory ), but you can choose whichever you like. Be consistent with the preferred version.

What is append slash in Django?

Among Django's many built-in features is APPEND_SLASH, which by default is set to True and automatically appends a slash / to URLs that would otherwise 404. Note: Web browsers aggressively cache past URLs and will often automatically add a trailing slash / as a result.


1 Answers

One of Django’s core design philosophies is URLs should be beautiful.

So some url like accounts/detail?name='abc' should be mapped as accounts/detail/abc/. You can capture it using regex at your url configurations. Here the URL is pretty neat and user friendly. This will help the search engines to index your pages correctly (now you can forget about rel=canonical) and will help in seo.

Now the reason for a trailing slash, consider a view (in any framework) which relatively resolves about.html for a user at path, users/awesomeUser

since users/awesomeUser and users/awesomeUser/ are different,

  1. If the user is at users/awesomeUser, the browser will resolve it as users/about.html because there ain't a trailing slash which we don't want

  2. If the user is at users/awesomeUser/, the browser will resolve it as users/awesomeUser/about.html because there is a trailing slash

  • child relative to family/parent/ is family/parent/child.
  • child relative to family/parent is family/child.

Django Design philosophy on Definitive URLs reads,

Technically, foo.com/bar and foo.com/bar/ are two different URLs, and search-engine robots (and some Web traffic-analyzing tools) would treat them as separate pages. Django should make an effort to “normalize” URLs so that search-engine robots don’t get confused.

This is the reasoning behind the APPEND_SLASH setting. (APPEND_SLASH lets you force append slashes to a URL)

Still not convinced?

  1. Since django observes both the urls as different, if you are caching your app, Django will keep two copies for same page at user/awesomeUser and user/awesomeUser/.
  2. You gotta have issues with HTTP methods other than GET if you don't append slash to a URL (If you ever plan to build a REST API).

Update

You can't make POST/PUT/PATCH/DELETE methods to work with rest_framework unless you explicitly define APPEND_SLASH=False in settings and trailing_slash=False for each and every router you gotta use(if you use Routers). It is like you basically gonna skip this most times and you gotta waste a hell lot of time debugging this. Django recommends append slashes and doesn't force it.

Its up to the developer to append slashes or not.

like image 187
All Іѕ Vаиітy Avatar answered Sep 20 '22 05:09

All Іѕ Vаиітy