简体   繁体   English

TypeError: __init__() 得到了一个意外的关键字参数“文件”

[英]TypeError: __init__() got an unexpected keyword argument 'file'

I'm using Django to develop a platform where users can upload files.我正在使用 Django 开发一个用户可以上传文件的平台。 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.我真的不明白我做错了什么。

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

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或者您必须创建模型SingleEnd的实例而不是表单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) (但是您使用表单SingleEndForm这会产生问题)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError at '' __init__() 得到一个意外的关键字参数 '' - TypeError at '' __init__() got an unexpected keyword argument '' Tensorboard TypeError:__ init __()得到一个意外的关键字参数'file' - Tensorboard TypeError: __init__() got an unexpected keyword argument 'file' Scrapy错误:TypeError:__ init __()得到一个意外的关键字参数'deny' - Scrapy Error: TypeError: __init__() got an unexpected keyword argument 'deny' TypeError:__init __()得到了意外的关键字参数错误 - TypeError: __init__() got an unexpected keyword argument error TypeError:__ init __()在argparse中有一个意外的关键字参数'type' - TypeError: __init__() got an unexpected keyword argument 'type' in argparse 图片/创建时发生TypeError-__init __()得到了意外的关键字参数'save' - TypeError at images/create - __init__() got an unexpected keyword argument 'save' TypeError:__init __()获得了意外的关键字参数'n_components' - TypeError: __init__() got an unexpected keyword argument 'n_components' TypeError:__init __()获得了意外的关键字参数'cv' - TypeError: __init__() got an unexpected keyword argument 'cv' TypeError:__ init __()得到一个意外的关键字参数'delay' - TypeError:__init__() got an unexpected keyword argument 'delay' TypeError:__ init __()得到一个意外的关键字参数'log_dir' - TypeError: __init__() got an unexpected keyword argument 'log_dir'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM