I have some issue with showing the image in django admin, it works well in local server, but got error 404 in production server… it seems something about the media root or pathing in setting… but i dont know, hope i can get an enlightment here…
so let’s start with my model to save the image…
def progress_photos_upload_path(instance, filename):
return os.path.join('image/bap/progress', instance.nama_proyek, filename)
def kwitansi_photos_upload_path(instance, filename):
return os.path.join('image/bap/kwitansi', instance.nama_proyek, filename)
class TerminPembayaran(models.Model):
#other fields
foto_progress = models.ImageField(upload_to=progress_photos_upload_path,blank=True,null=True)
foto_kwitansi = models.ImageField(upload_to=kwitansi_photos_upload_path,blank=True,null=True)
and here’s my admin
@admin.register(TerminPembayaran)
class TerminPembayaranAdmin(admin.ModelAdmin):
## other admin settings
def display_foto_progress(self, obj):
if obj.foto_progress:
return format_html('<a href="{}" target="_blank"><img src="{}" style="max-height:50px; max-width:50px;" /></a>', obj.foto_progress.url, obj.foto_progress.url)
else:
return '(No Image)'
display_foto_progress.short_description = 'Foto Progress'
def display_foto_kwitansi(self, obj):
if obj.foto_kwitansi:
return format_html('<a href="{}" target="_blank"><img src="{}" style="max-height:50px; max-width:50px;" /></a>', obj.foto_kwitansi.url, obj.foto_kwitansi.url)
else:
return '(No Image)'
display_foto_kwitansi.short_description = 'Foto Kwitansi'
and it supposed to be like this in the admin interface
but with the same code, when i run in production server, its return error (not literally error but the image just not found.)
okay so now i suspect this could only be the problem of image path in settings, i will show you the settings.py related to static and media.
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = "static/"
STATICFILES_DIRS = [BASE_DIR / "static_root"]
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
anyone know about this? and how to fix this? thankyou in advance