簡體   English   中英

FileView.get() 缺少 1 個必需的位置參數:'request' - class FileView

[英]FileView.get() missing 1 required positional argument: 'request' - class FileView

所以我想調用 func 到 html。 但是當我點擊按鈕時,它有錯誤消息。 我想要的是,我可以將字節保存到字符串並將其保存到文件文本中。 f 包含b'5&\xd7\x8c' ,這是加密的結果

FileView.get() 缺少 1 個必需的位置參數:“請求”

我不知道怎么了,有人可以檢查一下嗎?

def create_file(f):
    with open("file.txt", 'w') as file:
        return file.write(str(f))

class FileView(View):

    def get(f, self, request, *args, **kwargs):
        return HttpResponse(f, content_type='text/plain', headers={'Content-Disposition': 'attachment; filename=file.txt'})

def homepage(request):
    form = AudioForm()
    if request.method == "POST":
        form = AudioForm(request.POST, request.FILES)
        if form.is_valid():
            form.save()
            last_audio = Audio_store.objects.all().last()
            plaintext = Audio_store.objects.all().values_list('password').last()
            key = Audio_store.objects.all().values_list('key').last()
            pt = plaintext[0]
            ky = key[0]
            print(pt)
            print(ky)
            context={'form':form, 'last_audio':last_audio}
            enc = encrypt(ky, pt)
            print(enc)
            download = create_file(enc)
            print(download)
            return render(request, "homepage.html", context)

    context={'form':form}
    return render(request, "homepage.html", context=context)

在網址中:

path("", views.homepage, name="homepage"), # to call homepage
path("", views.encrypt, name="homepage"),
path("create_file", views.FileView.as_view(), name="create_file"),

在 html 中調用:

<a href="{% url 'create_file' %}">
                                            Download
                                        </a>

以下是錯誤的; 請查看 python 類。

class FileView(View):

    def get(f, self, request, *args, **kwargs):
        return HttpResponse(f, content_type='text/plain', headers={'Content-Disposition': 'attachment; filename=file.txt'})

第一個參數必須始終是self ->

class FileView(View):

    def get(self, *args, **kwargs):
        # Request is not needed in the arguments. Access it with:
        self.request
        # Generate the file here. 
        # Where did you expect it to appear from?
        return HttpResponse(f, content_type='text/plain', headers={'Content-Disposition': 'attachment; filename=file.txt'})

https://docs.python.org/3/tutorial/classes.html#class-and-instance-variables

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM