0

Hello I have a project where I want user to upload a title and body text and a picture named question, however when I submit the form only the title and body are saved the picture not. I changed the form tag in my template but it didn't help. Thanks in advance.

models.py    

class Question(models.Model):
    author = models.ForeignKey(User, on_delete= models.CASCADE,related_name='question_author')
    question=models.ImageField(upload_to='question/',blank=True,name="question")
    created_on = models.DateTimeField(auto_now_add=True)
    slug = models.SlugField(max_length=20, unique=True)
    title = models.CharField(max_length=128)
    body = models.CharField(max_length=400)

    class Meta:
        ordering = ['-created_on']

    def save(self, *args, **kwargs):
        self.slug = self.slug or slugify(self.title)
        super().save(*args, **kwargs)


    def __str__(self):
        return self.title


class Answer(models.Model):
    author = models.ForeignKey(User, on_delete= models.CASCADE,related_name='answer_author')
    question = models.ForeignKey('Question', on_delete=models.CASCADE, related_name='question_answer')
    answer=models.ImageField(upload_to='question/',blank=True)
    created_on = models.DateTimeField(auto_now_add=True)
    body = models.CharField(max_length=400)

    class Meta:
        ordering = ['-created_on']


    def get_absolute_url(self):
        return reverse("comment_detail",kwargs={'pk':self.pk})

forms.py

class QuestionForm(forms.ModelForm):
    class Meta:
        model=Question
        fields=['question','author','title','body']

class AnswerForm(forms.ModelForm):
    class Meta:
        model=Answer
        fields=['author','answer','body']

views.py

class QuestionDetail(FormMixin, generic.DetailView):
    model = Question
    template_name = 'question_detail.html'
    context_object_name = 'question'
    form_class = AnswerForm

    def get_context_data(self, **kwargs):
        context = super(QuestionDetail, self).get_context_data(**kwargs)
        context ['anwsermodel_list'] = Answer.objects.filter(question=self.object).order_by('created_on')
        context['form'] = AnswerForm(initial={'question': self.object})
        return context

    def post(self, request, *args, **kwargs):

question.html

       {%if user.is_authenticated%}
             <form method="post" style="margin-top: 1.3em;">
                 {% csrf_token %}
         <label for="post">Başlık:</label>
         <br>
         <input type="text" name="title" size=50 maxlength="128">
                 <br><br>
                 <label for="body">Soru:</label>
                 <br>
                 <textarea id="textarea" maxlength="400" name="body" rows="8" cols="50"></textarea>
                 <div id="textarea_feedback"></div>
                 <br>
                 <input type="file" name="question" accept="image/*" id="id_question">
                 <div class="modal-footer">
                     <button type="submit" class="btn btn-primary">Paylaş</button>
                     <button type="submit" class="btn btn-secondary" data-dismiss="modal">Kapat</button>
                 </div>
             </form>
       {%else%}
       <p style="text-align:center;">Soru sormak için <a style="text-decoration:none;" href="{% url 'account:login' %}?next={{ request.path }}">Giriş</a> yapmalısın.</p>
       {% endif %}
         </div>

I changed the form part from my template to {{form}} where I could see the form and submit it but again the picture were not uploaded.

1 Answer 1

1

You need to specify enctype attribute on your form:

<form method="post" style="margin-top: 1.3em;" enctype="multipart/form-data">

Otherwise, request.FILES will be empty.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.