Django adds app name (e.g., /blog) to static and image URLs — how to fix?
I'm working on a Django project, and I have a problem with static and image URLs. Django keeps adding /blog
(my app name) before every static or image URL, even though my files are in the global static/
folder.
For example:
I expect: /static/css/blog/bootstrap.css
Django generates: /blog/static/css/blog/bootstrap.css
This causes a 404 error because the /blog/static/
path doesn't exist.
Here is my setup:
settings.py:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
BASE_DIR / "static",
]
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / "media"
Project structure:
project/
├── static/
│ ├── css/
│ │ └── blog/
│ │ └── bootstrap.css
│ └── images/
├── Templates/ # Global templates directory
│ └── base.html
├── blog/
│ ├── urls.py
│ ├── views.py
Template:
<link rel="stylesheet" href="{% static 'css/blog/bootstrap.css' %}" />
<img src="{% static 'images/example.jpg' %}" alt="Example">
urls.py:
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('main.urls')),
path('blog/', include('blog.urls')),
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
The Problem :
All static and image URLs are being prefixed with /blog
, like this:
/blog/static/css/blog/bootstrap.css
How can I stop Django from adding the /blog
prefix to static and image URLs?
Thank you for your help!