Thank you for the perspective, David. That is good to know--that media and static are so different. Yes, in engineering, a separation of functions is often beneficial to the system. Same with software, I suppose.
However, the Django docs on managing files at https://docs.djangoproject.com/en/4.1/topics/files/ are very thin, tbh. They don't even display an example of rendering a user-uploaded image. They just do some REPL-type stuff. I am, as far as the extent of my knowledge goes, following the instructions on media_root and media_url, and they make it seem like it should be really simple. Either that or they're leaving out huge chunks of logic. I wouldn't put it past them that it be the latter.
And for all those involved and the lord and savior, I am not yet on production. FYI.
Michael
On Tuesday, April 11, 2023 at 11:58:23 PM UTC-7 David Nugent wrote:
It is very important to understand the difference between "static" and "media". The two are somewhat similar, and the code to handle (serve) each is even almost identical. Conceptually though they are quite different things.
static files are assets provided by your app and served to satisfy the direct needs of your app - e.g. javascript, css and other assets which never change, updated or rendered.
media files are more fluid things, usually uploaded or processed files, objects delivered by the application that are not a core part of rendering web pages.
Both STATIC_URL and MEDIA_URL are set up in your settings. In production, either or both may be served externally to django.
By default, MEDIA serving is not configured in Django settings, but there are many mentions of static files with a dozen or so possible configuration items. These are there because such configuration is important right at the start of developing a Django app.
If you need information on media handling though, you need to go back to the Django docs as there are no hints on how to do this in the default generated configuration.
The point is - do not confuse the two, and definitely do not try to tangle both as it will eventually lead to problems. Configure media separately from static and you will not encounter any of those problems, especially so when you get to deploying to production where you may wish to handle them differently (serving from cloud vs locally and choice of what served each etc and a myriad of other choices).
So, back to your problem, I would guess that you are simply missing a url handler for your media files at path('media/',…(, and the path stored in your db is almost definitely correct. The handler you will probably use in development mode will look something like your dev static files config, except for MEDIA_URL and pointing to the file system location where they are actually being uploaded.
HTH,
David
From: Michael Starr <starr...@gmail.com>
Reply: django...@googlegroups.com <django...@googlegroups.com>
Date: 12 April 2023 at 18:00:56
To: Django users <django...@googlegroups.com>
Subject: Image uploaded to database but not displaying
I am finding that the url of files stored in a database with Django is automated to /media/[filename], which is neat, because that keeps it organized and from images being everywhere. However, only the alt text of my file shows--the url is correct but the image doesn't show. I've tried using static, as well as just {{ image.url }} and neither method worked for me. Also, which method is correct? Can we frikkin decide already? :PThank you.Michael--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/4d1c1c52-c4d8-4f46-8333-adc673095fc1n%40googlegroups.com.
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/ad9095fc-f0f0-4d8a-bff6-2a6e3426c869n%40googlegroups.com.
No comments:
Post a Comment