The simple idea of the app I am developing is user give the Linux commands and the result of Linux command will be shown in the webbrowser. Here's my views.py:
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.template import RequestContext
import subprocess
globalcmd = 0
globalresult = 0
def ls(request):
if request.method == 'POST':
globalcmd = request.POST.get('command', False)
globalresult = subprocess.call(['globalcmd'], shell=True)
return HttpResponseRedirect('/thanks/')
else:
pass
return render_to_response('form.html', {'cmd':'cmd'}, context_instance=RequestContext(request))
def show_template(request):
return render_to_response('thanks.html', {'globalok':globalresult}, context_instance=RequestContext(request))
I get input from form.html which is processed by view 'ls'. As a user I am just testing with ls command. Whenever I press ls command it is processed by suprocess.call and stored in globalresult which is later called in thanks.html. My output is 0. What am I doing wrong? Here's thanks.html:
<h1>
{{ globalresult }}
</h1>
Check the documentation of the function you are calling , the result is the return-code of the invocation, not the output of the command itself. So, I think that your code does exactly what it should.
Maybe you intended to call subprocess.check_output ?
As a side note, be very careful with this web-terminal interaction; if you expose this web application to the internet without proper security bad things will happen.... but you probably know that.
you're not passing globalresult
to the thanks.html template in show_template()
you probably want
return render_to_response('thanks.html', {'globalresult':globalresult}, context_instance=RequestContext(request))
if lazy you can also do
return render_to_response('thanks.html', locals(), context_instance=RequestContext(request))
...though it seems like you're trying to do a redirect to get to the thank you page (?). In that case, it would be better to just render the thank you view immediatley
eg replace this line
return HttpResponseRedirect('/thanks/')
with either of the two lines above ^ in ma answer
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.