Search code examples
djangourl-routing

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


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),
]

Solution

  • $: 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.