Enabling cache and images not loading in django?

If you enable the cache:

@cache_page(20, key_prefix='index_page')
def index(request):
 post_list = Post.objects.order_by('-pub_date').all()
 paginator = Paginator(post_list, 10) 
 page_number = request.GET.get('page')  
 page = paginator.get_page(page_number)  
 return render(request, "index.html", {'page': page, 'paginator': paginator})

When I create a new post, I can no longer upload an image.

@login_required
def post_edit(request, username, post_id):
  profile = get_object_or_404(User, username=username)
  post = get_object_or_404(Post, pk=post_id, author__username=username)

if request.user != post.author:
  return redirect('post', username=username, post_id=post_id)

form = PostForm(request.POST or None, files=request.FILES or None, instance=post)
if form.is_valid():
   form.save()
   return redirect('post', username=username, post_id=post_id)

return render(request, 'post_new.html', {'form': form, 'post': post})

How can I fix this so that it was possible to download pictures and use the cache?

Welcome @Vettel12 !

We need a more detailed explanation of what exactly is happening here.

Which of these two views aren’t behaving as you expect?

What is not happening that you are expecting to see happen? (Or, what isn’t happening that you are expecting to see?)

Are there any error messages being reported?

I discovered that even without a cache, images are not loaded when creating a new post. The situation is this: when I create a post on the admin page, the post appears in the database with a picture, but when I go through the post creation form on the site, the post appears without a picture.

I’m writing an autotest. And the picture appears there too.

But when I click edit post, the picture loads.

When creating a post there is no image, when editing (def post_edit) it loads normally.

def new_post(request):
    if request.method == 'POST':
        form = PostForm(request.POST, request.FILES)
        if form.is_valid():
            group = form.cleaned_data.get("group")
            text = form.cleaned_data.get("text")
            post = Post(group=group, text=text, author=request.user)
            post.save()
            return redirect("index")
    else:
        form = PostForm()  
    return render(request, "new.html", {"form": form})
class Post(models.Model):
    text = models.TextField()
    pub_date = models.DateTimeField("date published", auto_now_add=True)
    author = models.ForeignKey(User, on_delete=models.CASCADE, related_name="posts")
    group = models.ForeignKey(Group, on_delete=models.SET_NULL, related_name="posts", blank=True, null=True)

    image = models.ImageField(upload_to='posts/', blank=True, null=True, widget=forms.FileInput())

    def __str__(self):
       return self.text

I use the same page form for editing and for creating a new post. Added to it:

<form method="post" enctype="multipart/form-data">

I read on the Internet, but everything seems to be in order. What could be the problem?

What does your PostForm look like?

class PostForm(ModelForm):
    class Meta:
        model = Post
        labels = {'text': 'Text', 'group': 'Group'}
        help_texts = {'text': 'Text', 'group': 'Group'}
        fields = ['group', 'text', 'image']

Ok, you’re using a model form, which means your handling of that form should be more along the lines of your post_edit view above:

if form.is_valid()
    form.save()
    ...

You should not be creating an instance directly.

Also see The save() method docs for using the commit=False parameter in the save.