1๐
I implemented using inlineformset_factory
forms.py
...
from django.forms import inlineformset_factory
...
ImagesFormSet = inlineformset_factory(
Post,
PostImages,
fields=("image",),
extra=4,
max_num=4,
widgets={
"image": forms.FileInput(attrs={"class": "form-control col-md-4 col-sm-4 d-inline"}),
},
labels={
"image": "",
},
)
and in my views.py
send ImagesFormSet with get_context_data
as
from .forms import UpdateBlogForm, ImagesFormSet
...
class BlogUpdateView(generic.UpdateView):
model = Post
form_class = UpdateBlogForm
success_url = "."
template_name = "blog/dashboard_components/blog_dashboard_eidtpost.html"
def get_context_data(self, **kwargs):
context = super(BlogUpdateView, self).get_context_data(**kwargs)
context["images_form"] = ImagesFormSet(instance=get_object_or_404(Post, id=2))
return context
and finally accessed ImagesFormSet as images_form in my template.
๐คAli Aref
Source:stackexchange.com