[英]Django views.py function
我試圖在單擊 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})
我也有一個錯誤說
TypeError at /external/
'bool' object is not iterable
當我在本地服務器上運行它時。
我的 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()
在您看來,您寫道:
out=run(sys.executable==['//D://Desktop//new//file1//test.py',inp], shell=False,stdout=PIPE)
但這沒有多大意義:您在這里將sys.executable
(它是一個字符串)與一個字符串列表進行比較,因此它將返回False
。 所以你然后調用run(False, shell=False, stdout=PIPE)
,但False
當然不是字符串。
您可以將其重寫為:
out=run([sys.executable, '//D://Desktop//new//file1//test.py',inp], shell=False,stdout=PIPE)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.