YangTegap
YangTegap

Reputation: 431

Django | Cannot access URLs without a trailing "/"

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?

Upvotes: 0

Views: 225

Answers (2)

YangTegap
YangTegap

Reputation: 431

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.

Upvotes: 0

jaromir
jaromir

Reputation: 91

Please check APPEND_SLASH setting in the settings.py file.

Please also check that link:

django urls without a trailing slash do not redirect

Upvotes: 4

Related Questions