Home > front end >  TypeError: __init__() got an unexpected keyword argument 'file'
TypeError: __init__() got an unexpected keyword argument 'file'

Time:06-06

I'm using Django to develop a platform where users can upload files. This function was working fine for months with no issues, but for some reason now I get this error when trying to upload a file:

Traceback:

Traceback (most recent call last):
  File "/home/me/.local/lib/python3.8/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
  File "/home/me/.local/lib/python3.8/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/me/project/app/views.py", line 314, in invoke_local
    instance = SingleEndForm(file=request.FILES['file'])

Exception Type: TypeError at /invoke_local/
Exception Value: __init__() got an unexpected keyword argument 'file'

My model:

class SingleEnd(models.Model):

    file = models.FileField(upload_to="documents/")
    email = models.CharField(max_length=100)

    def __str__(self):
        return self.email

My form:

class SingleEndForm(forms.ModelForm):
    class Meta:
        # shows which model to use from models.py
        model = SingleEnd
        # fields = '__all__'
        fields = ["file", "email"]

        labels = {
            "file": "input your fasta/fastq file (min 3 sequences)",
            "email": "input your email to get a notification for your results in a timely manner",
        }
        widgets = {
            "email": forms.EmailInput(attrs={"class": "form-control"}),
            "file": forms.FileInput(attrs={"class": "form-control"}),
        }

My view:

def invoke_local(request):
    # path to save inputs
    media_path = "/home/me/project/media/documents/"
    # path to save outputs
    result_path = "/home/me/project/media/results"
   
    if request.method == "POST":
        # use the form to upload form info (post) and files
        form = SingleEndForm(request.POST, request.FILES)
        if form.is_valid():
            # saves full form
            instance = SingleEndForm(file=request.FILES['file'])
            instance.save()

            # changes file name if the name is the same
            file_name_final = instance.file.name[10:]
            # final path
            file_path = media_path   file_name_final
    else:
        raise Http404("Form not entered correctly")
form = SingleEndForm()
return render(request, "invoke_local.html", {"form": form})

I really don't get what I've done wrong.

CodePudding user response:

Based on example in documentation Handling uploaded files with a model you mix two different methods and this makes problem

You can directly write Form

form = SingleEndForm(request.POST, request.FILES)
if form.is_valid():
    # saves full form
    form.save()

Or you have to create instance of model SingleEnd instead of form SingleEndForm

form = SingleEndForm(request.POST, request.FILES)
if form.is_valid():
    # use model 
    instance = SingleEnd(file=request.FILES['file'])
    instance.save()

(but you use form SingleEndForm and this makes problem)

  • Related