Django TypeError: Ожидается объект str, bytes или os.PathLike, а не NoneType

Я пишу веб-страницу, которая может запускать скрипт python по нажатию кнопки на html. Вот что у меня есть: views.py

from django.shortcuts import render
import requests
import sys
from subprocess import run,PIPE


def external(request):
    inp= request.POST.get('param')
    out= run([sys.executable,'test.py',inp],shell=False,stdout=PIPE)
    print(out)
    return render(request,'home.html',{'data1':out.stdout})

urls.py

from django.urls import path 
from . import views

# urlconf
urlpatterns = [
    path('external/', views.external)
]

home.html

<html>
    <head>
        <title>
        Python button script
        </title>
    </head>
    <body>
        <form action="/external/" method="post">
        {% csrf_token %}
        Input Text:
        <input type="text" name="param" required><br><br>
        {{data_external}}<br><br>
        {{data1}}
        <br><br>
        <input type="submit" value="Execute External Python Script">
        </form>
    </body>
</html>

скриншот каталога

Проблема заключается в этой строке кода: out= run([sys.executable,'test.py',inp],shell=False,stdout=PIPE) из views.py. Как я могу исправить это? Спасибо!

Вероятно, вы используете этот view(external) и для метода POST, и для метода GET, и у вас нет данных в методе get, поэтому вы получаете ошибку.

попробуйте это:

def external(request):
    if request.method == "POST":
        inp= request.POST.get('param')
        out= run([sys.executable,'test.py',inp],shell=False,stdout=PIPE)
        print(out)
        return render(request,'home.html',{'data1':out.stdout})
    else:
        return render(request,'home.html')
Вернуться на верх