Nikul Panchal
Nikul Panchal

Reputation: 1673

django admin view image file not found

i am new here in django, I have uploaded image file, i can see file is uploaded under my app myapp/mysite/uploads/testimonial/image.jpg file is there, so it is correct, but now when i open that testimonial module in admin, and when i edit that testimonial and click on that view image link it is showing that as this link http://127.0.0.1:8000/uploads/uploads/testimonial/image.jpg, but that is not visible, can anyone please help me why that image is not showing, i am using python version 3.6, here i have mention whole my code, can anyone please look into it and help me to resolve this issue ?

models.py

class Testimonial(models.Model):

    def __str__(self):
        return self.title

    TESTIMONIAL_STATUS = ((1, 'Active'), (0, 'Inactive'))
    title = models.CharField(max_length=255)
    description = HTMLField()
    image = models.ImageField(upload_to='uploads/testimonial')
    status = models.PositiveSmallIntegerField(choices=TESTIMONIAL_STATUS, default=1)
    published_date = models.DateTimeField(auto_now_add=True, blank=True)

settings.py

MEDIA_ROOT = os.path.join(BASE_DIR, 'uploads/')
MEDIA_URL = '/uploads/'

urls.py

urlpatterns = [
    path('', views.index, name='index'),
]

Upvotes: 1

Views: 1000

Answers (1)

PythonSherpa
PythonSherpa

Reputation: 2600

Change your upload_to field to:

image = models.ImageField(upload_to='testimonial')

Remember upload_to is a path relative to MEDIA_ROOT. The string value will be appended to your MEDIA_ROOT path.

If you want to try it out, follow the steps in Serving files uploaded by a user during development

Upvotes: 1

Related Questions