My UpdateView working but I am not understating why file is not uploading in my CreateView?
here is my code:
froms.py
class BlogForm(ModelForm):
class Meta:
model = Blog
fields = ['title','body']
ImageFormSet = inlineformset_factory(Blog,BlogHeaderImage,fields=('image',),extra=1)
views.py
class BlogCreate(CreateView): #file is not uploading in create view?
model = Blog
template_name = 'blog_post.html'
form_class = BlogForm
def get_context_data(self, **kwargs):
kwargs['latest_posts_list'] = Blog.objects.order_by('-id')
return super(BlogCreate, self).get_context_data(**kwargs)
def get_context_data(self, **kwargs):
data = super().get_context_data(**kwargs)
if self.request.POST:
data["children"] = ImageFormSet(self.request.POST, self.request.FILES)
else:
data["children"] =ImageFormSet()
return data
def form_valid(self, form):
form.instance.author = self.request.user
context = self.get_context_data()
children = context["children"]
self.object = form.save()
if children.is_valid():
children.instance = self.object
children.save()
return super().form_valid(form)
def get_success_url(self):
return reverse("blog")
class BlogUpdate(UpdateView): # Updateview is working and image file is uploading
model = Blog
template_name = 'blog_update_post.html'
#fields = ['author','title','body']
form_class = BlogForm
def get_context_data(self, **kwargs):
data = super().get_context_data(**kwargs)
if self.request.POST:
data["children"] = ImageFormSet(self.request.POST, self.request.FILES, instance=self.object)
else:
data["children"] =ImageFormSet(instance=self.object)
return data
def form_valid(self, form):
context = self.get_context_data()
children = context["children"]
self.object = form.save()
if children.is_valid():
children.instance = self.object
children.save()
return super().form_valid(form)
def get_success_url(self):
return reverse("blog")
I am also not getting any errors. After click on update button I successfully redirected in Blog page without uploading file.