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??
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.
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.
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.
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.
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,
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
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 tofamily/parent/
isfamily/parent/child
.child
relative tofamily/parent
isfamily/child
.
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)
user/awesomeUser
and user/awesomeUser/
.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With