1

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?

civilian
  • 147
  • 1
  • 12
  • `"no file chosen"` is the default placeholder for file inputs. You need to click it to add a file (image in this case). There's nothing missing, your code is (should be) working – yuvi Sep 22 '18 at 02:22
  • I mean i already uploaded an image when creating the post, i need that image to show as chosen but it does not show. – civilian Sep 22 '18 at 02:27
  • Can you check in the shell if the file is saved or not? i.e. query a post and see if image has any value. That would tell you if the problem is with *saving* the data or *displaying* it – yuvi Sep 22 '18 at 02:28
  • The show part of the blog shows the image uploaded. The image is already in disk and can be displayed if i use post.image.url but the form does not show it. – civilian Sep 22 '18 at 02:32
  • If i do  in the template it displays the image but the file chooser just shows: "no file chosen". – civilian Sep 22 '18 at 02:37
  • You have answered your own question in an edit - you can't set a value on a file input. – solarissmoke Sep 22 '18 at 06:16
  • 1
    Doesn't No file chosen stand for the Choose file button, which is meant for the selection of new image to upload and Currently: shows currently saved and used image with its path? – Uroš Trstenjak Sep 22 '18 at 07:12

2 Answers2

0

When you define your POST method in either class-based views or function-based views you retrieve the form using request.POST.

if you have files like pictures in your form you have to mention that in your views like this :

form = self.form(request.POST,request.FILES) # class based view.
AbrarWali
  • 607
  • 6
  • 19
0

In image = models.ImageField(blank=True, null=True), specify location to upload your image like

image = models.ImageField(blank=True, null=True,upload_to="relativepathtoimages/")

Hanna
  • 11
  • 2