I
I
Ivanjo1012020-12-17 10:41:54
Django
Ivanjo101, 2020-12-17 10:41:54

Is it possible to upload a txt file that was created in a function?

I'm new to Django I have
this question
I'm submitting data via an html form
It has fields: name and file itself The file
is an archive
I wrote a function that works with this archive, reads its contents and overwrites it in a new txt file. This txt file is saved in the project folder
The question is: is it possible to do something so that this txt file, which is created by the function, is loaded into the Django database itself?
Under this file, I created a model with a FileField field type, but I don’t know how to enter information. Help

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
gromyko21, 2020-12-17
@Ivanjo101

If it's reading the content, I'm assuming it's doing it with a loop (line by line), and I'm also assuming you have a Text model with a text field. You can do something like this if you want to record the entire text:

text_list = []
for line in some_file:
    text_list.append(line)
new_text = Text.objects.create(text=text_list)

In general, it is better to get acquainted with the possibilities of working with documents Link . And read the documentation on django create method

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question