Home > Back-end >  uploading multiple files in django form and function in views
uploading multiple files in django form and function in views

Time:03-30

I am trying to upload multiple files in Django form. I followed multiple methods. I am successful via models to views but I want via models to forms to views. I followed Use view/form in Django to upload a multiple files this question but my form is not submitting. Here I am sharing my codes. I hope to get the mistake I am doing by any expert.

#models.py

class Question(models.Model):
    description = models.TextField(blank=True, null=True)
    created_by = models.ForeignKey(User, blank=False, on_delete=models.CASCADE)

    def __str__(self):
        return self.description


class QuestionFile(models.Model):
    question = models.ForeignKey(
        Question, on_delete=models.CASCADE, related_name='files', null=True, blank=True)
    file = models.FileField(
        'files', upload_to=path_and_rename, max_length=500, null=True, blank=True)

    def __str__(self):
        return str(self.question)


#forms.py
from django import forms
from .models import *


class QuestionForm(forms.ModelForm):
    class Meta:
        model = Question
        fields = ['description']


class QuestionFileForm(QuestionForm):  # extending Questionform
    file = forms.FileField(
        widget=forms.ClearableFileInput(attrs={'multiple': True}))

    class Meta(QuestionForm.Meta):
        fields = QuestionForm.Meta.fields   ['file', ]

    def clean(self):
        cleaned_data = super().clean()
        description = cleaned_data.get("description")
        file = cleaned_data.get("file")

        if not description and not file:
            self.add_error(
                'description', 'Kindly describe the question details or upload any file')


#views.py
def questionView(request):
    if request.method == 'POST':
        form = QuestionFileForm(request.POST or None, request.FILES or None)
        files = request.FILES.getlist('file')
        if form.is_valid():
            question = form.save(commit=False)
            question.created_by = request.user
            # add everything needed to add here
            question.save()
            if files:  # check if user has uploaded some files
                for file in files:
                    QuestionFile.objects.create(question=question, file=file)

            messages.success(request, 'Question asked successfully.')
            return redirect('url_name')
    else:
        form = QuestionFileForm()
    context = {
        "page_title": "Ask a Math Question",
        "form": form
    }
    return render(request, 'template.html', context)


#template.html
{% extends 'base.html' %}
<!-- title -->
{% block title %}{{ page_title }}{% endblock title %}

<!-- body -->
{% block content %}
<div >
  <!--for message-->
  {% if messages %} {% for message in messages %}
  <div
    {% if message.tags %}
    
    {% endif %}
  >
    {{ message }}
  </div>
  {% endfor %} {% endif %}

  <form
    method="POST"
    action="{% url 'fask_question' %}"
    novalidate
    
    enctype="multipart/form-data"
  >
    {% csrf_token %}

    <div >
      <input
        type="file"
        
        name="files"
        id="exampleFormControlFile1"
        multiple
      />
    </div>

    <div >
      <textarea
        
        id="exampleFormControlTextarea1"
        rows="8"
        placeholder="Type your question here"
        name="description"
      ></textarea>
    </div>

    <button type="submit" >Submit</button>
  </form>
</div>
{% endblock %}

I am unable to understand where I am making the mistake that my form is not submitting. If anyone can help I will be grateful. Thanks in advance.

CodePudding user response:

Try to pass the form from the context in to the template

Change this part of your template

<form
    method="POST"
    action="{% url 'fask_question' %}"
    novalidate
    
    enctype="multipart/form-data"
  >
    {% csrf_token %}

    <div >
      <input
        type="file"
        
        name="files"
        id="exampleFormControlFile1"
        multiple
      />
    </div>

    <div >
      <textarea
        
        id="exampleFormControlTextarea1"
        rows="8"
        placeholder="Type your question here"
        name="description"
      ></textarea>
    </div>

    <button type="submit" >Submit</button>
  </form>

To

  <form
    method="POST"
    action="{% url 'fask_question' %}"
    novalidate
    
    enctype="multipart/form-data"
  >
    {% csrf_token %}
  {{ form }}

    <button type="submit" >Submit</button>
  </form>
  • Related