"Это поле обязательно для заполнения", когда все поля заполнены в Django(5.0.6)

Итак, я столкнулся с ошибкой при разработке проекта django, и кто-то задал тот же вопрос 5 лет назад, поэтому stackoverflow не позволяет мне задать его снова.

Вот в чем вопрос.

"Это поле обязательно", когда все поля заполнены в Django

Но этот ответ не исправил ошибку для меня. Текущий ответ устарел и требует пересмотра с учетом последних изменений.

view.py

from django.shortcuts import render,redirect
from .forms import ProductForm

# Create your views here


def Upload(request):
    if request.method == 'POST':
        form = ProductForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('home')
    else:
        form = ProductForm()

    if request.user.is_superuser:
        return render(request, r'../templates/upload.html', {'form': form})
    else:
        return redirect('home')

forms.py

from django import forms
from .models import Product


CategoryChoices = {
        ('Overwhelming', "Overwhelming"),
        ('Outrageous', "Outrageous"),
        ('Mnmlsm', "Mnmlsm"),
        ('Gothic', "Gothic")
    }

class ProductForm(forms.ModelForm):
    title = forms.CharField(widget=forms.Textarea)
    image1 = forms.FileField(label='select an image')
    image2 = forms.FileField(label='select an image',required=False)
    image3 = forms.FileField(label='select an image',required=False)
    image4 = forms.FileField(label='select an image',required=False)
    image5 = forms.FileField(label='select an image',required=False)
    category = forms.ChoiceField(choices=CategoryChoices)
    price = forms.IntegerField()
    price.widget.attrs.update({'placeholder': 'Enter Price'})
    title.widget.attrs.update({'placeholder': 'Enter product title'})

    class Meta:
        model = Product
        fields = ['title', 'image1', 'image2', 'image3','image4','image5','category','price']

upload.html

{% extends 'base.html' %}

{% block content3 %}
<title>Henkan - Login</title>
{% endblock %}
{% block content1 %}
    <div class="popup">
        <h1 class="middle_align">Upload new product</h1>
        <form method="post" class="middle_align" enctype="multipart/form-data">
            {% csrf_token %}

            {% if form.errors %}
                {% for field in form %}
                    {% for error in field.errors %}
                        <div class="alert middle_align">
                        <div class="closebtn" onclick="this.parentElement.style.display='none';">&times;</div>
                            {{ error }}
                        </div>
                    {% endfor %}
                {% endfor %}
            {% endif %}
            <div class="middle_align" style="resize: none;">{{form.title}}</div>
            <div class="middle_align">{{form.image1}}</div>
            <div class="middle_align">{{form.image2}}</div>
            <div class="middle_align">{{form.image3}}</div>
            <div class="middle_align">{{form.image4}}</div>
            <div class="middle_align">{{form.image5}}</div>
            <div class="middle_align">{{form.category}}</div>
            <div class="middle_align">{{form.price}}</div>
            <button type="submit">Upload</button>
        </form>
    </div>
    <div class="loginfill"></div>
{% endblock %}
{% block login %}<a href="{% url 'register' %}">signup</a>{% endblock %}

models.py

from django.db import models
import re

# Create your models here.

def slugify(s):
  s = s.lower().strip()
  s = re.sub(r'[^\w\s-]', '', s)
  s = re.sub(r'[\s_-]+', '-', s)
  s = re.sub(r'^-+|-+$', '', s)
  return s

class Product(models.Model):

    CategoryChoices = {
        ('Overwhelming', "Overwhelming"),
        ('Outrageous', "Outrageous"),
        ('Mnmlsm', "Mnmlsm"),
        ('Gothic', "Gothic")
    }

    title = models.TextField()
    image1 = models.FileField(upload_to = r'media/product_media',blank=True,null=True)
    image2 = models.FileField(upload_to = r'media/product_media',default=None,blank=True,null=True)
    image3 = models.FileField(upload_to = r'media/product_media',default=None,blank=True,null=True)
    image4 = models.FileField(upload_to = r'media/product_media',default=None,blank=True,null=True)
    image5 = models.FileField(upload_to = r'media/product_media',default=None,blank=True,null=True)
    url_id = slugify(str(title))
    category = models.CharField(
        max_length=25,
        choices=CategoryChoices,
    )
    price = models.IntegerField()

Вернуться на верх