Right, what is so difficult for Django to work out in pertinence to displaying an image on a template when DEBUG
is set to False?
Why is it, when I do literally everything correctly, does it just outright refused to display the bloody image on the page?
Why is it, when I have correctly set up my MEDIA_URL
and my MEDIA_ROOT
in my settings.py
, does it still refuse to load the page? Why is it that despite correctly setting up my root urls.py
page in the following fasion, does Django refuse to the display the image in the webpage:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Why is is that, despite correctly setting up the upload_to
directly in my models.py
file, run the makemigrations
as well as the migrate
command, does Django….you get the idea.
Why is that that despite installing whitenoise
, and running the py manage.py collectstatic
command doesnt Django…oh yes, sorry, we’ve done this before.
Why is is that when I go to “inspect source” in my web browser does it return the correct image directory, yet when I click on the image URL directory from the inspect source page, does it return my beautifully-crafted custom 404 page? I know, shocker right? Something actually works! Nearly fell off of my chair there!
Just why?
Why is it that no matter what I do, I still get the same result? Why is it that no matter what leading or trailing slashes I add or remove from my MEDIA_URL
and my MEDIA_ROOT
variables in my settings.py
page,. do I still get the CORRECT image URL on my inspect source
yet the image still does not display? Why is that not matter how much of my sanity I give away, do I still get the same result?
I have made every single standard diagnostic procedure known to the universe, I have read the Django documentation, I have even resorted to standing on one leg, hopped up and down and sung the hokey cokey song in hopes that it will actually coax Django into doing as it’s told.
I am sick of it,
I am sick of it,
I am sick of it,
sick of it,
sick of it,
sick of it,
I am so tired it is unreal.
This is my code:
#urls.py
"""
URL configuration for blauwestadtech project.
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/4.2/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include
from . import views
from django.conf import settings
from django.conf.urls.static import static
from django.views.static import serve
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index),
....
]
# Only for development. Do not use this in production.
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
#models.py
from django.db import models
# Create your models here.
class MediaModule(models.Model):
...
....
article_image = models.ImageField(upload_to='assets/')
...
...
...
#settings.py
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media'
#template.html
<div class="article-container__article-image">
<img src="{{article.article_image.url}}" alt="{{article.article_image.url}}"/>
</div>
I’m sorry but this is so logical, it is blatantly obvious and that the settings.py
tells Django that the Base Directory is the media
file, which is the name of the app, the upload_to
tells Django where to specifically upload the tile to, within the media
app, ergo, the directory is ROOT > media > assets
, which is exactly what the directory is showing when I inspect the source of the web page.
Why does Django find it such a terrible hardship to just display the image?
I am not a stupid person, yet Django seems to just insult my intelligence.
Added slashes, removed slashes, added slashes, removed slashes, re-added another slash as at this point I was questioning my own sanity and could not remember if added a trailing slash, added a leading slash, removed a leading slash or removed a trailing slash, removed a trailing slash, removed a leading slash or just simple had a nervous breakdown, inspect the source of the website to confirm the directory with which the browser is (apparently) reading from.
And yes, I am oh so painfully aware that there are a billion questions with a similar issue on here, they were either posted 56 years ago and since Django has made a significant amount of version releases and thus rendering said solutions obsolete, or the answers given on the more recent posts, 55 years ago, were grossly inaccurate.
JoJoJohan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.