Question

I am new to Django I'm stuck with regards to how I can fix this issue.

I have a a model which contains an ImageField as follows

name = models.CharField(max_length=100)
description = models.CharField(max_length=1000)
file = models.ImageField(upload_to='img')

and then in my settings.py I have

STATIC_ROOT = os.path.join(PROJECT_PATH, "static")
STATIC_URL = '/static/'

MEDIA_ROOT = os.path.join(PROJECT_PATH, 'media')
MEDIA_URL = 'media/'

and I have the following url pattern to handle this

 url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
                            'document_root': settings.MEDIA_ROOT, 'show_indexes' : True
                            })

In the admin pages I am able to upload the image perfectly fine to the expected location in the media folder, and I am able to access this by loading 127.0.0.1:8000/media/img/my_image.jpg. The problem is that in the standard admin panel the link which shows the currently uploaded image for the object simply appends media/img/my_img.jpg to the end of the current URL and therefore receives a 404.

I am not sure what the best way to fix this issue is, I Hope this makes sense and please let me know if more information is needed.

Thanks.

Was it helpful?

Solution

Change

MEDIA_URL = 'media/'

to

MEDIA_URL = '/media/'

and it should stop appending to the URL and replace it instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top