Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django urls without a trailing slash do not redirect

I've got two applications located on two separate computers. On computer A, in the urls.py file I have a line like the following:

(r'^cast/$', 'mySite.simulate.views.cast') 

And that url will work for both mySite.com/cast/ and mySite.com/cast. But on computer B I have a similar url written out like:

(r'^login/$', 'mySite.myUser.views.login') 

For some reason on computer B the url mySite.com/login/ will work but mySite.com/login will hang and won't direct back to mySite.com/login/ like it will on computer A. Is there something I missed? Both url.py files look identical to me.

like image 517
whatWhat Avatar asked Oct 20 '09 18:10

whatWhat


People also ask

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

Do URLs need a 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.

Which method is used instead of path () in URLs py to pass in regular expressions as routes?

If the paths and converters syntax isn't sufficient for defining your URL patterns, you can also use regular expressions. To do so, use re_path() instead of path() .

What is Re_path in Django?

re_path is a callable within the django. urls module of the Django project.


2 Answers

Or you can write your urls like this:

(r'^login/?$', 'mySite.myUser.views.login') 

The question sign after the trailing slash makes it optional in regexp. Use it if for some reasons you don't want to use APPEND_SLASH setting.

like image 138
Michael Gendin Avatar answered Oct 01 '22 16:10

Michael Gendin


check your APPEND_SLASH setting in the settings.py file

more info in the django docs

like image 36
Jiaaro Avatar answered Oct 01 '22 15:10

Jiaaro