[英]Django views.py function
I am trying to run an enternal python script upon clicking a button in a django website, however I think the path of my external script which i have specified is in the wrong formatting:我试图在单击 django 网站中的按钮时运行内部 python 脚本,但是我认为我指定的外部脚本的路径格式错误:
from django.shortcuts import render, render_to_response
from subprocess import run,PIPE
import requests
import sys
from django.views.decorators.csrf import csrf_exempt
# Create your views here.
def index(request):
return render_to_response('index.html')
@csrf_exempt
def external(request):
inp=request.POST.get('param')
out=run(sys.executable==['//D://Desktop//new//file1//test.py',inp], shell=False,stdout=PIPE)
print(out)
return render(requests, "index.html",{'data1':out})
I also have an error which says我也有一个错误说
TypeError at /external/
'bool' object is not iterable
when I run it on the local server.当我在本地服务器上运行它时。
My urls.py file:我的 urls.py 文件:
from django.contrib import admin
from django.urls import path
from myapp import views as v
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = [
path('admin/', admin.site.urls),
path('', v.index, name="index"),
path("external/", v.external),
]
urlpatterns += staticfiles_urlpatterns()
In your view, you wrote:在您看来,您写道:
out=run(sys.executable==['//D://Desktop//new//file1//test.py',inp], shell=False,stdout=PIPE)
But that makes not much sense: you are here comparing sys.executable
(which is a string), with a list of strings, and therefore it will return False
.但这没有多大意义:您在这里将
sys.executable
(它是一个字符串)与一个字符串列表进行比较,因此它将返回False
。 So you then call run(False, shell=False, stdout=PIPE)
, but False
is of course not a string.所以你然后调用
run(False, shell=False, stdout=PIPE)
,但False
当然不是字符串。
You can rewrite this to:您可以将其重写为:
out=run([
sys.executable, '//D://Desktop//new//file1//test.py',inp], shell=False,stdout=PIPE)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.