0

I have a Django application where, for some reason, I cannot load my URLs without a trailing "/"

I have other Django applications I've made in practice where this hasn't been an issue, and I don't need one when I go to my Admin page, so I can't figure out what's setup wrong here.

Here's my main URLs file:

urlpatterns = [
    path('app/', include('app.urls')),
]

Here's my app's URLs file:

urlpatterns = [
    path('subapp/', views.subapp, name='subapp'),
]

And my views file:

def subapp(request):
    return render(request, 'app/subapp.html', {'title': 'subapp'})

When I enter my URL as "localhost:8000/app/subapp/" it takes me to the correct page.

However, when I enter my URL as "localhost:8000/app/subapp" I get a 404 with the following debug error message: Directory indexes are not allowed here.

What am I doing wrong?

2
  • Your path subapp/ has a slash at the end. Commented Feb 4, 2021 at 23:48
  • @KlausD. Yes, but I have CommonMiddleware going, so it should append the slash, shouldn't it? I want there to be a slash, because the issue is, if I go to "localhost:8000/app" without the slash, it also won't load... On my other Django projects, the slash is appended. Commented Feb 4, 2021 at 23:51

2 Answers 2

4

Please check APPEND_SLASH setting in the settings.py file.

Please also check that link:

django urls without a trailing slash do not redirect

Sign up to request clarification or add additional context in comments.

1 Comment

I had checked this, but for my specific use case, it was because I was missing MEDIA_ROOT = os.path.join(BASE_DIR, 'media')and MEDIA_URL = '/media/'
0

For my specific usecase, it was because I needed to add the following to my settings.py:

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

This fixed my issue.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.