Tuesday, April 11, 2023

Re: Image uploaded to database but not displaying

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 <starrtennis@gmail.com>
Reply: django-users@googlegroups.com <django-users@googlegroups.com>
Date: 12 April 2023 at 18:00:56
To: Django users <django-users@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? :P

Thank 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+unsubscribe@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.

No comments:

Post a Comment