Hi, I don’t understand why my form shows me this message: This field is required
here join my model:
class Shop(models.Model):
name = models.CharField(max_length=50, default=None)
description = models.TextField(default=None)
market = models.ForeignKey(Market, on_delete=models.CASCADE)
user = models.ForeignKey(User, on_delete=models.CASCADE)
image = models.ImageField(
verbose_name='image',
upload_to='shop',
default=None
)
def __str__(self):
return self.name
here join my form
from django import forms
from django.forms import ModelForm
from django.db import models
from .models import Category, Shop, Article, Market
from user.models import User
class ShopForm(ModelForm):
class Meta:
model = Shop
exclude = ('user', 'market')
class CategoryForm(ModelForm):
class Meta:
model = Category
fields = '__all__'
class ArticleForm(ModelForm):
class Meta:
model = Article
fields = '__all__'
here join my views:
@login_required(login_url='/user/')
@authors_vendor
def newshop(request):
if request.method == "POST":
shop = Shop(
user=request.user,
market=Market.objects.first()
)
form = ShopForm(request.POST, instance=shop)
if form.is_valid():
form.save()
form = ShopForm()
else:
form = ShopForm(request.POST)
else:
form = ShopForm()
context = {'form': form}
template = loader.get_template('newshop.html')
return HttpResponse(template.render(context, request))
here join my templates:
{% extends "dashboard.html" %}
{% load bootstrap5 %}
{# Load CSS and JavaScript #}
{% bootstrap_css %}
{% bootstrap_javascript %}
{# Display django.contrib.messages as Bootstrap alerts #}
{% bootstrap_messages %}
{% block content %}
<form action="{% url 'newshop' %}" method="post">
{% csrf_token %}
{% bootstrap_form form %}
{% buttons %}
<button type="submit" class="btn btn-primary">
Enregister
</button>
{% endbuttons %}
</form>
{% endblock %}