Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: Is APPEND_SLASH set to True even if not in settings.py?

Tags:

python

django

web

Here is my urls.py:

urlpatterns = [
    url(r'^polls/', include('polls.urls')),
    url(r'^admin/', admin.site.urls),
]

If I type 127.0.0.1:8000/polls in my browser (Firefox) I get redirected to 127.0.0.1:8000/polls/ (with slash at the end).

To be honest, I am confused why that is. Because in my settings.pythere is no

APPEND_SLASH = True

However, the Django docs say:

APPEND_SLASH

Default: True

When set to True, if the request URL does not match any of the patterns in the URLconf and it doesn’t end in a slash, an HTTP redirect is issued to the same URL with a slash appended. Note that the redirect may cause any data submitted in a POST request to be lost.

Source: https://docs.djangoproject.com/en/1.11/ref/settings/#append-slash

Is it that APPEND_SLASH is True by default even if it's not in settings.py? And that you should only put it in this file if you want to set it to False? Or what's the reason for that behaviour?

like image 955
Aliquis Avatar asked Aug 20 '17 16:08

Aliquis


People also ask

What happens if you skip the 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.

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.

Which is default database in settings file in Django?

Which is default database in settings file in Django? By default, the configuration uses SQLite. If you're new to databases, or you're just interested in trying Django, this is the easiest choice. SQLite is included in Python, so you won't need to install anything else to support your database.

When Allowed_hosts configuration is empty host is validated against?

When DEBUG is True and ALLOWED_HOSTS is empty, the host is validated against ['.localhost', '127.0.0.1', '[::1]'] . ALLOWED_HOSTS is also checked when running tests. This validation only applies via get_host() ; if your code accesses the Host header directly from request.META you are bypassing this security protection.


1 Answers

Yes, the default is True, so if you don't provide it in your settings file it will be True.

Any setting that is not defined in your settings file will use the default value, which is provided in django.conf.global_settings.

like image 180
knbk Avatar answered Oct 30 '22 14:10

knbk