Django FileField: File not uploading in media folder or sql database

32 Views Asked by At

I'm doing a e-learning portal using Django. The user 'teacher' should be able to upload files under any course. However, the files are not being upload in media/course_materials folder. When i check the sql database, there is no instance of the id being created when a file is upload (i tried both an image file and a word document file, both of very less storage).

This is the debugging comment i get from views :

" <MultiValueDict: {'course_materials': [<InMemoryUploadedFile: filematerial.docx (application/vnd.openxmlformats-officedocument.wordprocessingml.document)>]}>"

models:

class Course(models.Model):
    name = models.CharField(max_length=100)
    description = models.TextField(default ="Description of Course")
    course_materials = models.ManyToManyField('CourseMaterial', blank=True)

    def __str__(self):
        return self.name
    
class CourseMaterial(models.Model):
    name = models.CharField(max_length=255)
    file = models.FileField(upload_to='course_materials/')

    def __str__(self):
        return self.name

views:

def edit_course(request, course_id):
    course = get_object_or_404(Course, id=course_id)
    if request.method == 'POST':
        form = CourseForm(request.POST, request.FILES.get, instance=course)
        if form.is_valid():
            print(request.FILES)
            course = form.save(commit=False)
            course.save()
            form.save_m2m()  # Save many-to-many relationships
            return redirect('teacher')  # Redirect to teacher dashboard after editing the course
    else:
        form = CourseForm(instance=course)
    
    context = {
        'form': form,
        'course': course,
    }
    return render(request, 'elearn/edit_course.html', context)

forms.py:

class CourseForm(forms.ModelForm):
    class Meta:
        model = Course
        fields = ['name', 'description', 'course_materials']

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['course_materials'].queryset = CourseMaterial.objects.none()

html:

<form method="post" enctype="multipart/form-data">
        {% csrf_token %}
        <div class="mb-3">
            <label for="id_name" class="form-label">Course Name</label>
            {{ form.name }}
        </div>
        <div class="mb-3">
            <label for="id_description" class="form-label">Description</label>
            {{ form.description }}
        </div>
        <div class="mb-3">
            <label for="id_course_materials" class="form-label">Course Materials</label>
            <ul>
                {% for material in course.course_materials.all %}
                <li>{{ material.name }}: <a href="{{ material.file.url }}">{{ material.file.name }}</a></li>
                {% endfor %}
            </ul>

            <input label="course_materials_input" type="file" name="course_materials" class="form-control-file">
        </div>
        <button type="submit" class="btn btn-primary">Save Changes</button>
    </form>

i have also added these in setting.py:

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
1

There are 1 best solutions below

0
nlewis99 On

In your view, change:

request.FILES.get

To:

request.FILES

Also worth noting that media will not be directly stored in a relational database. The DB just contains a reference of where the media is stored.