9

I'm using django admin on my website. When I enter url without slash after admin (http://example.com/admin) I receive 404 error. I thought that django automatically added slash on the end of url. Of course when I enter url ended with slash it works fine. What I am doing wrong, or which settings I have to change. Thanks for any ideas.

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
lukasz
  • 153
  • 1
  • 1
  • 6

2 Answers2

10

Try setting APPEND_SLASH = True in settings.py.

On second thoughts, I think the default setting is True.

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

David Wolf
  • 1,400
  • 1
  • 9
  • 18
Sriram
  • 838
  • 7
  • 15
  • 2
    Thanks for quick replay. I have put APPEND_SLASH into settings (commonmidelware also is putted) but unfortunately still the same. maybe mod_python has problem with redirecting from 'admin' into 'admin/'? – lukasz Dec 14 '10 at 10:41
  • I noticed that this happend to all link on my website. not olny admin. When I enter section name not ended with slash, slash didn't add automatically (the difference is that I don't have 404 but it is coused by rule in urls.py) – lukasz Dec 14 '10 at 10:47
  • Does this happen when you use django's built in webserver or only when you run on Apache? – Sriram Dec 14 '10 at 10:52
  • Stop using mod_python. I don't know if that's the reason you're having problems, but support for mod_python is deprecated and will be removed in Django 1.5 (http://docs.djangoproject.com/en/dev/howto/deployment/modpython/). – Dominic Rodger Dec 14 '10 at 11:05
  • Sriam, I have to check it. Now I only work on mod_python. Thanks. Dominic thanks for info, I will try to change server into mod_wsgi but it depends on my host provider. – lukasz Dec 14 '10 at 11:23
  • Problem has bessn solved. My mistake in urlsy.py. Thank you for your advieces. – lukasz Dec 15 '10 at 11:01
  • @lukasz can you tell what was the mistake because am having the same problem – Ahmed Wagdi Jan 29 '22 at 08:56
1

Don't forget the CommonMiddleware in your settings.py:

It's important to remember that the APPEND_SLASH parameter works in conjunction with the 'django.middleware.common.CommonMiddleware'. So in order for it to work you should have the following in your settings.py:

MIDDLEWARE = [
    'django.middleware.common.CommonMiddleware',
]

You don't need to add the APPEND_SLASH in your settings.py because the default behavior is to redirect URLs that you've typed without and ending slash, for the correct one and as a pattern, you should always write your URLs with an ending slash, like:

urlpatterns = [
    path('hello/', views.hello_world),
]
marcelfox
  • 101
  • 6