Skip to content

File uploads from frontend don't carry through to Django #82

@eddyojb88

Description

@eddyojb88

I've followed the guide on the Inertia site and I've tried a few things. I don't think Django-inertia is handling file uploads properly as I can never see the content on the server side in request.FILES.

Here is my example React code:


import React from 'react';
import { useForm } from '@inertiajs/react'; 

export default function FileUploader() {
    const { post } = useForm();  

    const handleFileUpload = (e) => {
        const file = e.target.files[0];
        if (!file) return;

        const formData = new FormData();
        formData.append('archimate_file', file);

        post('/projects/upload', formData, {
            forceFormData: true,
            onSuccess: () => {
                alert('Upload successful');
                e.target.value = ''; // reset input
            },
            onError: (errors) => {
                console.error('Upload error:', errors);
            },
        });
    };

    return (
        <div>
            <label className="cursor-pointer bg-blue-600 hover:bg-blue-700 text-white px-4 py-2 rounded">
                Upload Archimate File
                <input
                    type="file"
                    accept=".archimate"
                    onChange={handleFileUpload}
                    className="hidden"
                />
            </label>
        </div>
    );
}

In Django:

views.py

@login_required
@require_http_methods(["POST"])
def upload_archimate_project(request):
    if 'archimate_file' not in request.FILES: 
        print("Files in request:", request.FILES)  
        print("POST data:", request.POST)
        return JsonResponse({'error': 'No file uploaded'}, status=400)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions