-1

I want to run my script (makes changes to my database and creates users on another website using an API) whenever a user presses the button on my website. So far i have tried these solutions but they are not working for me.

How can i trigger a Python script in background from HTML tag button, in Django?

Run python script in Django website when clicking on button submit

The view

def external(request):
    out = run([sys.executable, 'C:\\Users\\hafiz\\PycharmProjects\\DotsPrototype\\proto\\moodle.py'], shell=False, stdout=PIPE)
    print(out.stdout)
    return render(request, 'proto/index.html', {'data1': out.stdout})

The error

  return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'DotsPrototype'
b''
.
.
.
/external/vendor/jquery/jquery.min.js

1 Answers1

0

You need to create a view that will handle the POST request on button click. The view will have the logic to call your script. Not sure if you creating REST APIs or a monolithic application. In both the cases, the approach remains the same.

Atul Mishra
  • 1,898
  • 2
  • 13
  • 18
  • I did exactly that but it seems to get into the script and gives errors on the imports that i have made and says the connection is aborted – Syed Muhammad Ibrahim Jun 01 '20 at 14:23
  • Can you provide us your view please. It is tough to solve a coding issue without code :p – JSRB Jun 01 '20 at 14:23
  • @SyedMuhammadIbrahim It's tough to figure out the issue this way. It's better if you add your current code and the traceback of the error you are getting. – Atul Mishra Jun 01 '20 at 14:54
  • @Jonas I am happy to provide the code but there is very minimal information about the code being referred to in the question. No code in the question hence no code in the answer :P – Atul Mishra Jun 01 '20 at 14:58
  • @AtulMishra I was refering to the OP, ye^^ we can't help that way – JSRB Jun 01 '20 at 15:05