Using subprocess in Django to run Java (HelloWorld) file

47 views
Skip to first unread message

Alex Wan

unread,
Aug 3, 2020, 8:56:30 PM8/3/20
to Django users

Upon form submission from index.html in templates, I want to call the view function to run HelloWorld.java (in same directory as the admin, apps, models, etc files) and return the result onto the webpage.

So the form code would be something like

<form action="{% url path.to.request_page %}" method="POST">
    <input id="submit" type="button" value="Click" />
</form>
The view function (same as from the link)

import subprocess

def executeJava():
  
    s = subprocess.check_output("javac HelloWorld.java;java HelloWorld", shell = True)
    print(s.decode("utf-8"))


How do I translate the driver function into Django? Insert into a function in views similar to this?

def form_submit(request):
if request.method == 'POST'
executeJava()
return render(request, 'templates/index.html')

Do I have to map this into urls.py? If so, how?
Reply all
Reply to author
Forward
0 new messages