-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
4
  • What is your Script. Did you defined routes and business logic? Commented Jun 1, 2020 at 13:20
  • What in particular does not work? You have to call a view function after clicking the button to initialize the backend logic. Commented Jun 1, 2020 at 13:25
  • @Jonas- It seems to get into the script but gives errors on the imports that i have made and says the connection is aborted. Commented Jun 1, 2020 at 14:21
  • @Jonas - I did define the routes. Just got to know about business logic though. Commented Jun 1, 2020 at 14:22

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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
Can you provide us your view please. It is tough to solve a coding issue without code :p
@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.
@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
@AtulMishra I was refering to the OP, ye^^ we can't help that way

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.