简体   繁体   中英

how to customize ModelForm filled data

I have this model:

class Gallery(models.Model):
   HeadImage = models.ImageField(upload_to="gallery")

With this form:

class GalleryForm(ModelForm):
   class Meta:
       model = Gallery

And this view:

gform = GalleryForm(request.POST, request.FILES, instance=galleryInstance)

In template a filled form is shown. For the HeadImage field it shows a link to an image related to the HeadImage field, and a fileinput with a change label:

{{ gform.HeadImage }}

Now instead of a link to the picture, I want to put the image into an img tag. I do this in the template as follows:

<img src={{ gform.HeadImage.value }}/>

What should I do so that the link doesn't show in the form?

To prevent it from showing, use any of these three options:

  1. Set editable=False on the model field;
  2. Use the fields attribute of the ModelForm 's inner Meta class.
  3. Use the exclude attribute of the ModelForm 's inner Meta class.

Which one to use depends on how often you want the field to show (never, or in select cases). See the Django docs for more information on this.

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