Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
1 Odpowiedz
6473 Widoki

hi, I'm trying to write a function that should create a text, writing on it and the downloading it, can someone explain me how to achieve this?


thanks in advance, every help is appreciated

Awatar
Odrzuć
Najlepsza odpowiedź

Hi Cristian,

=> If you want to create directly your text file, you can do that:


with open("My file.txt", "w") as file:
    file.write("Your text goes here")
 
=> If you want to download your file by a button:

You have to add this fileds in your model "model_name":
your_file = fields.Binary("My file")
Then use this script:
import base64
data = "Your text goes here"
self.your_file = base64.b64encode(data.encode())

return {
'type': 'ir.actions.act_url',
'url': '/web/content/model_name/%s/your_file/%s?download=true' % (
self.id, "My file.txt"),
}


Best regards,

Imal-Tech


Awatar
Odrzuć
Autor

Thank you, it's finally clear, I've the last doubt, It gives me this error "module_name has no attribute your_file", how could I solve it?

The model name (2) is the one on which you're working like product.product. Now, you have to add the field in your model on which you're working, this one: your_file = fields.Binary("My file").
Precisely, you've to to put your file on your model name, and the custom_model on the function name (1).

Check the attached image on the main response above, and you'll get each part with its matched code.

If it works, don't hesitate to validate the response, thanks!

Autor

okay, now it's working, but when I click the button it returns a 404 Error, the URL is not found

Cristian, you've to modify:

'url': '/web/content/model_name/%s/custom_model/%s?download=true' % (
to:
'url': '/web/content/model_name/%s/your_file/%s?download=true' % (

Powiązane posty Odpowiedzi Widoki Czynność
0
lis 22
76
2
wrz 17
15128
0
mar 15
3998
0
cze 23
2889
3
cze 20
8776