inlineformset_factory fields is rendering in my html template. Others fields data saving except inlineformset_factory fields. here is my code:
models.py
class HeaderImage(models.Model):
header_image = models.ImageField()
post = models.ForeignKey(Post, on_delete=models.CASCADE)
froms.py
BlogImageFormSet = inlineformset_factory(Post, # parent form
HeaderImage, # inline-form
fields=['header_image'] ,can_delete=False, extra=1)
#views.py
class BlogUpdateView(PermissionRequiredMixin,UpdateView):
raise_exception = True
permission_required = "blog.change_post"
model = Post
template_name = "blog_update_post.html"
form_class = BlogPost
def get_success_url(self):
self.success_url = 'http://127.0.0.1:8000/blog'
return self.success_url
def get_context_data(self, **kwargs):
context = super(BlogUpdateView, self).get_context_data(**kwargs)
if self.request.POST:
context['form'] = BlogPost(self.request.POST, instance=self.object)
context['ingredient_form'] = BlogImageFormSet(self.request.POST, instance=self.object)
else:
context['form'] = BlogPost(instance=self.object)
context['ingredient_form'] =BlogImageFormSet(instance=self.object)
return context
def post(self, request, *args, **kwargs):
self.object = self.get_object()
form_class = self.get_form_class()
form = self.get_form(form_class)
ingredient_form = BlogImageFormSet(self.request.POST)
if (form.is_valid() and ingredient_form.is_valid()):
return self.form_valid(form, ingredient_form)
else:
return self.form_invalid(form, ingredient_form)
def form_valid(self, form, ingredient_form):
self.object = form.save()
ingredient_form.instance = self.object
ingredient_form.save()
return HttpResponseRedirect(self.get_success_url())
def form_invalid(self, form, ingredient_form):
return self.render_to_response(
self.get_context_data(form=form,
ingredient_form=ingredient_form,
))
I am not understanding why data is not saving for inlineformset_factory field. see the picture for better understand