W
W
withoutname2020-09-24 17:02:19
Django
withoutname, 2020-09-24 17:02:19

How to call a function from views.py file (django)?

The idea is the following: on the html page there is a form with an input tag and a text attribute. There are several such fields. When the "send" button is pressed, all the data entered by the user in an array goes to the server in the views.py file, in which the external (request) function:

def external(request):
    global z
    z = request.POST.getlist("items")
    run([sys.executable, 'C://Users//support//PycharmProjects//untitled//project//main//codetest.py'])

    return render(request, 'main/test.html')

takes an array like this: z = request.POST.getlist("name") and also runs the codetest.py script. The variable z is declared globally (global z).
In the same file (views.py) there is another test() function, in which the external() function is called and the z array is converted to a tuple:
def test():
    external()
    tuplez = tuple(z)

In the codetest.py file, I import the function from the views.py file as follows: from main.views import test, where main is the name of the django application. Then, at a specific place in the code, I call this function.
The problem is that when I run the script, I get the following message: ModuleNotFoundError: No module named 'main', although the files __init__.py, views.py and codetest.py (script) are in the same directory.
What could be the problem?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
withoutname, 2020-09-24
@withoutname

Solved the problem as follows: Added variable z
to run([sys.executable, 'C://Users//support//PycharmProjects//untitled//project//main//codetest.py', str (z)]) , which takes on an array. Called this variable as an argument in the script: x = "%s" % (sys.argv[1]).

V
Vladimir, 2020-09-25
@AstraVlad

from .views import test ?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question