Why does a URL containing '/' after a '$' return a Page Not Found Error (404)

Viewed 72

I'm having an issue with Django URL patterns.

When I add a '/' to the end of the index URL, the page returns a 404 error (Page Not Found) and if I remove the '/' from the end of the URL then the page works fine.

The issue is not reproducible with the URL for the admin page, can someone explain what's going on?

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$/',index),
]
4 Answers

$: Represents end of string, so, there's no posibility for a char living after it.

Matches the end of the string or just before the newline at the end of the string

^ matches the start of the string so, ^$ in root urls.py means to Django: I don't want anything in my URL except the domain / base name then Django will route the request to your index page.

url method of django.conf.urls package accept regex as first parameter

$ represent end of string in regular expression hence any char after that will not be considered to match url string.

As I understood , in django urls:

'$' : End of the string, this is use in regex

'^' : It is also use in regular expression is matching starting of url

no characters after $ will be considered in the url pattern hence change this to :

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^/$',index),
]
Related