I am trying to make an inlineforms through django. I have made a model to save the uploaded Image. Unfortunately the image is not getting uploaded through the form. I am not able to track why the image is not getting saved in the folder.
models.py - This file contains all the models.
```
class courses(models.Model):
name = models.CharField(max_length = 50)
class topics(models.Model):
name = models.CharField(max_length = 50)
sno = models.PositiveIntegerField(default = 0)
course = models.ForeignKey(courses,on_delete = models.CASCADE,null = True)
class sub_topics(models.Model):
name = models.CharField(max_length = 50)
content = models.TextField(null = True)
sno = models.PositiveIntegerField(default = 0)
image = models.ImageField(upload_to=('images/'),blank= True,null = True)
topic = models.ForeignKey(topics,on_delete = models.CASCADE,null = True)
```
forms.py - In this file through the inlineformset_factory I have made courses->topics->sub_topics data base structure. The sno is use to store the serial number of the child forms
```
TopicFormset = inlineformset_factory(courses, topics, fields=('name','sno'),extra =1,widgets = {'sno': HiddenInput()})
Sub_TopicFormset = inlineformset_factory(topics, sub_topics, fields=('name','content','sno','image'),extra = 1,widgets = {'sno': HiddenInput()})
```
views.py - In this file I have used CreateView to display the forms created. While saving the results the Course and Topics are saved but while saving the subtopic only the character input is getting saved. The image is not getting saved Even after uploading the image. The image is also not getting saved in the media/images folder.
```
class Create_Course_View(CreateView):
model = courses
form_class = CourseForm
template_name = 'create/add.html'
success_url = 'create/add.html'
def form_valid(self, form):
result = super(Create_Course_View, self).form_valid(form)
topic_formset = TopicFormset(form.data, instance=self.object, prefix='topic_formset')
if topic_formset.is_valid():
topics = topic_formset.save()
topics_count = 0
for topic in topics:
sub_topic_formset = Sub_TopicFormset(form.data, instance = topic, prefix='sub_topic_formset_%s'% topics_count)
x = sub_topic_formset.cleaned_data
print(x[0])
if sub_topic_formset.is_valid():
sub_topic_formset.save()
topics_count += 1
return result
def get_context_data(self, **kwargs):
context = super(Create_Course_View, self).get_context_data(**kwargs)
context['topic_formset'] = TopicFormset(prefix='topic_formset')
context['sub_topic_formset'] = Sub_TopicFormset(prefix='sub_topic_formset_0')
return context
class upload_image(CreateView):
model = image
form_class = Upload
template_name = 'create/upload.html'
def form_valid(self, form):
result = super(upload_image, self).form_valid(form)
return result
def get_context_data(self, **kwargs):
context = super(upload_image, self).get_context_data(**kwargs)
return context
```
add.html
```
<h1>Add Course</h1>
<form action="" method="post" enctype="multipart/form-data" >
{% csrf_token %}
{{ form.as_p }}
<div id="topics">
{{ topic_formset.management_form }}
{% for form in topic_formset %}
<fieldset class="topic_form">
<legend>Topic</legend>
{{ form.as_p }}
<div class="topic_sub_topics">
{{ sub_topic_formset.management_form }}
{% for form in sub_topic_formset %}
<fieldset class="topic_sub_topic_form">
<legend>Sub Topic</legend>
{{ form.as_p }}
</fieldset>
{% endfor %}
</div>
<p><input type="button" value="Add Sub Topic" class="btn_add_sub_topic" id="topic-{{ forloop.counter0 }}"/></p>
</fieldset>
{% endfor %}
</div>
<p><input type="button" id="btn_add_topic" value="Add Topic"/></p>
<p><input type="submit" value="Save"></p>
</form>
```