I have created a userUpdate view with a partial modelForm to update user data, which consists of an imageField.
Form in my template looks like:
<div class='field'>{{ form.photo.label_tag }} {{ form.photo}}</div>
Here photo is the imageField.
The rendered html view is:

But,
photo? So, that I can individually use them as required.You need to change the widget from ClearableFileInput to Fileinput
# forms.py
from django.forms.widgets import FileInput
class UserForm(forms.ModelForm):
class Meta:
model = User
fields = '__all__'
widgets = {
'photo': FileInput(),
}
The default FileField widget is
ClearableFileInput. https://docs.djangoproject.com/en/dev/ref/forms/widgets/#file-upload-widgets
Alternatively, You can render HTML manually for file type field.
<div class="field">
<!-- show label of field -->
{{form.photo.label_tag}}
<!-- check for input type -->
{% if form.photo.field.widget.input_type == 'file'%}
<a href="{{ MEDIA_URL }}{{ form.photo.value }}">{{ form.photo.value }}</a><br/>
<input type="file" name="{{ form.photo.name }}" />
{% endif %}
</div>
You can get URL of current image using . operator
# URL of the image
photo.url
# name of the image
photo.name
ImageFieldImageField inherits all attributes and methods from FileField, but also validates that the uploaded object is a valid image.
In addition to the special attributes that are available for FileField, an ImageField also has height and width attributes.
Read the official
docsfor the list of all attributes
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With