简体   繁体   中英

Django - default image not showing for imagefield

So I have a page on my website in which I am displaying an image and if the user does not upload an image a default image is assigned. At the moment I am only trying to get default images to work but I can't seem to get it. My code is below and I will explain my thought process after.

Settings.py

MEDIA_ROOT = '/home/joe/documents/exchange/Texchange/media/'

MEDIA_URL = '/media/'

html page

{{ posting.image.url }}
<img src="{{ MEDIA_URL }}postings/nophoto.jpg"/>

I have read about multiple methods for displaying the image so I was trying 2 at once.

models.py

    image = models.ImageField(upload_to='/home/joe/documents/texchange/media/postings/', default="../../static/textchange/nophoto.jpg")

The file path /home/joe/Documents/exchange/Texchange/media/postings/nophoto.jpg exists

The file path /home/joe/Documents/exchange/Texchange/static/textchange/nophoto.jpg exists

This is what shows up where the photo should be plus there is a missing image box besides this (for if an image isn't found): /media/nophoto_tzD4Uib.jpg

So I thought that once I allow users to upload photos they would go to ../media/postings and that I should store the default in static files. Then are unique ID's assigned to photos in media/postings because users will be uploading multiple pictures once I upload. Also, why isn't the default image showing up?

Thanks.

Such code will work for you.

MEDIA_ROOT = '/home/joe/documents/exchange/Texchange/media'

MEDIA_URL = '/media/'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM