I'm trying to create the edit of posts for a blog.
When i create the form with the instance all the fields display but the image part appears like "No file chosen"
Here is my model
class Post(models.Model):
title = models.CharField(max_length=255)
content = models.TextField()
image = models.ImageField(blank=True, null=True)
publication_date = models.DateField(default=datetime.date.today)
expiring_date = models.DateField()
Here is my form
class PostForm(forms.models.ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.instance.publication_date = datetime.date.today()
class Meta:
model = Post
exclude = ['publication_date']
widgets = {
'title': forms.fields.TextInput(attrs={
'class': 'form-control'
}),
'content': forms.Textarea(attrs={
'class': 'form-control'
}),
'image': forms.fields.FileInput(attrs={
'class': 'form-control'
}),
'expiring_date': forms.fields.DateInput(attrs={
'input_type': 'date',
'class': 'form-control'
}),
}
This is my view
def edit(request, post_id):
post = get_object_or_404(Post, pk=post_id)
form = PostForm(instance=post)
return render(request, 'posts/edit.html', {'form': form})
And here is my template
{% block content %}
<div class="row">
<div class="col-md-12">
<div class="card animated materialU animation-delay-5">
<div class="card-block card-block-big">
<form action="{% url 'posts:update' %}" method="post" enctype='multipart/form-data'>
{{ form }}
<input type="submit" name="save changes" value="Save Changes" class="btn btn-success">
{% csrf_token %}
</form>
</div>
</div>
</div>
</div>
{% endblock %}
What is missing?
Edit; Maybe is imposible because of security reasons?: How to set a value to a file input in HTML?