S o i want to called func to html. but when i click the button, it have error message. what i want is, i can save bytes to string and save it to file text. f contains with b’5&\xd7\x8c’ (it’s bytes) , it’s the result from encryption
FileView.get() missing 1 required positional argument: ‘request’
i don’t know what’s wrong, can someone check it please?
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)
in urls:
path("", views.homepage, name="homepage"), # to call homepage
path("", views.encrypt, name="homepage"),
path("create_file", views.FileView.as_view(), name="create_file"),
in html to called:
<a href="{% url 'create_file' %}">
Download
</a>