简体   繁体   中英

how to display in models img to html in django

I tried to display to html changeable profile image with admin. can someone explain please

models in Portfolio app:

class ProfileImage(models.Model):
  profile = models.ImageField(("Profile image"), upload_to=None, height_field=None, width_field=None, max_length=None)

this is base html in template folder (i tried this one):

<img src="{{ portfolio.models.ProfileImage.progile.url }}" alt="profile"><br />

You should first configure your model to store profile pictures. Your ProfileImage model looks correct, but you need to specify where the images should be uploaded using the upload_to argument of ImageField .

Include the model in your admin form by using a ModelForm or manually adding it to the admin's fields list.

In your HTML template, you can display the image using the url attribute of your template's ImageField object.

You have an error in your HTML above, it should be profile.url and not portfolio.models.ProfileImage.profile.url:

<img src="{{ profile.image.url }}" alt="profile">

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