Rest api errors in forms of django
For my project, I implemented the option to add articles for the user with rest api Now when I fill in the fields, it doesn't send and says you have to fill in the photo. What is problem? I'm gonna to give user add or edit of article option and for this plan , I created a page for that and I created form for add or edit; but i have a problem, when I fill fields and click button for send data, i got an error that wants fill image fields!
model: # Article model
class Article(models.Model):
title = models.CharField(max_length=37, verbose_name='عنوان')
category = models.ManyToManyField(Category, related_name='articles', verbose_name='دسته بندی')
text = models.TextField(verbose_name='متن')
images = models.ManyToManyField(Image, related_name='articles', verbose_name='تصاویر', blank=True, null=True)
main_image = models.ImageField(upload_to='article/main/images', blank=True, null=True, verbose_name='تصویر اصلی')
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
status = models.BooleanField(default=True, verbose_name='وضعیت')
slug = models.SlugField(blank=True, null=True, unique=True, verbose_name="اسلاگ")
class Meta:
verbose_name = "مقاله"
verbose_name_plural = "مقالات"
def save(self, force_insert=False, force_update=False, using=None,
update_fields=None):
self.slug = slugify(self.title)
super(Article, self).save()
def show_image(self):
if self.images:
return format_html(f'<img src="{self.images.url}" width="47px" height="37px">')
else:
return format_html('<h3 style="color: red">بدون تصویر</h3>')
show_image.short_description = ' تصویر'
def __str__(self):
return self.title
form: # form of add an article
class ArticleForm(forms.ModelForm):
class Meta:
model = Article
fields = ('title', 'category', 'text', 'main_image')
serializer: # serializer of article
class ArticleSerializer(serializers.ModelSerializer):
class Meta:
model = Article
fields = ('title', 'category', 'text', 'main_image')
view: # view of manage of article
class ArticleView(APIView):
def get(self, request):
form = ArticleForm
serializer = ArticleSerializer(instance=form, many=True)
return render(request, 'home/manage_of_article.html', {'form': form, 'serializer': serializer})
def post(self, request):
form = ArticleForm(request.POST)
if form.is_valid():
article_instance = form.save()
serializer = ArticleSerializer(instance=article_instance)
if serializer.is_valid():
serializer.save()
return Response({'Response': 'Added'}, status=status.HTTP_200_OK)
else:
return Response(form.errors, status=status.HTTP_400_BAD_REQUEST)
return render(request, 'home/manage_of_article.html', {'form': form})
urls:
urlpatterns = [
path('article_manage', views.ArticleView.as_view(), name='article_manage'),
path('article_manage/add', views.ArticleView.as_view(), name='add'),
]
html:
<form id="tm-contactform" class="tm-form" method="post">
{% csrf_token %}
<div class="tm-form-inner">
<div class="tm-form-field">
{{ form.as_p }}
</div>
<div class="tm-form-field">
<a href="{% url 'home:home' %}">
<button type="submit" class="tm-button">افزودن<b></b></button>
</a>
</div>
</div>
</form>