Добавление изображения для записей в блоге

Кодирую какой-то блог на django, и не могу сделать так, чтобы главная страница содержала изображения статей... Она просто не загружает изображение...

my Views.py :

class AddPostView(CreateView):
    model = Post
    form_class = PostForm
    template_name = 'add_post.html'

my Models.py:

class Post(models.Model):
    title = models.CharField(max_length=255)
    title_tag = models.CharField(max_length=255, default="YNTN")
    #author = models.ForeignKey(User, on_delete=models.CASCADE)
    body = RichTextField(blank=True, null=True)
    image = models.ImageField(upload_to="profile_pics", blank=True, null=True)
    #body = models.TextField()
    post_date = models.DateField(auto_now_add=True)
    likes = models.ManyToManyField(User, related_name="blog_posts")

    def total_likes(self):
        return self.likes.count()

    def __str__(self):
        return (self.title + " | " + str(self.author))

    def get_absolute_url(self):
        return reverse("home")

Мой Forms.py:

class PostForm(forms.ModelForm):
    class Meta:
        model = Post
        fields = ('title', 'title_tag', 'body', 'image')
        widgets = {
            'title': forms.TextInput(attrs={'class':'form-control', 'placeholder':'Title of the Blog'}),
            'title_tag': forms.TextInput(attrs={'class':'form-control', 'placeholder':'Copy the title with no space and a hyphen in between'}),
            'body': forms.Textarea(attrs={'class':'form-control', 'placeholder':'Content of the Blog'}),
        }

и мой add_post.html :

{% extends 'base.html' %}
{% block title %}Make an article{% endblock %}
{% block content %}

{% if user.is_authenticated %}

    <h1>Make an article</h1>
    <div class="form-group">
        <form method="POST">
            <br/>
            {% csrf_token %}
            {{ form.media }}
            {{ form.as_p }}
            <button class="btn btn-dark">POST</button>
    </div>
{% else %}
<h1>You are not allowed to post! You need to <a href="{% url 'login' %}">Log in</a> or <a href="{% url 'register' %}">Register</a></h1>
{% endif %}
{% endblock %}

Я перепробовал много способов, но ничего не получилось.

Вам не хватает enctype="multipart/form-data" (как указано в документации) в теге формы внутри html-шаблона. Обновите html-файл следующим образом:

<form method="POST" enctype="multipart/form-data">
        <br/>
        {% csrf_token %}

        {{ form.as_p }}
        <button type="submit" class="btn btn-dark">POST</button>
 </form>
Вернуться на верх