[英]Trying to run python script from a Django form
I am creating a simple ui which takes 2 files as input and if we press submit button then trying to run script.py (which is Python script) and render output on screen or some output folder我正在创建一个简单的用户界面,它需要 2 个文件作为输入,如果我们按下提交按钮,然后尝试运行 script.py(这是 Python 脚本)并在屏幕上或某些 Z78E6221F6393D1356Z81DBCE63 上渲染 output
#forms.py #forms.py
from django import forms
class AwsForm ( forms.Form ):
Aws_SourceFile = forms.FileField ()
Aws_RefernceFile = forms.FileField ()
#views.py #views.py
from django.shortcuts import render
from .forms import AwsForm
import subprocess
def home_view(request):
context = {}
context [ 'form' ] = AwsForm ()
return render ( request , "home.html" , context )
def submit_view(request):
info = request.POST [ 'info' ]
def my_view(request):
if request.method == 'GET':
form = AwsForm ()
else:
if form.is_valid ():
info = request.POST [ 'info_name' ]
output = script_function ( info )
return render ( request , 'myapp/home.html' , {
'info': info ,
'output': output ,
} )
return render ( request , 'myapp/home.html' , {
'form': form ,
} )
def script_function(post_from_form):
print ( post_from_form )
return subprocess.check_call ( [ '\script_18122020.py' , post_from_form ] )
#urls.py #urls.py
from django.contrib import admin
from django.urls import path
from myapp.views import home_view, submit_view
urlpatterns = [
path('', home_view ),
path('submit', submit_view),
]
#home.html #home.html
{% block main_content %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Homepage</title>
</head>
<body>
<h1>Welcome to Adwait Algorithm and Aws.</h1>
<form method="POST" enctype="multipart/form-data">
<form action="your_view_url" method="post">
{% csrf_token %}
{{ form.as_p}}
<input type="submit" value="Submit">
</form>
<p> Post Data: {{ info }} </p>
<p> Result: {{ output }} </p>
{% endblock main_content %}
</body>
</html>
I am trying to pickup a script and I want to display the output to a folder or on html file or on screen.我正在尝试获取脚本,并且我想将 output 显示到文件夹或 html 文件或屏幕上。
You will have to alter your script function to accept 2 files.您必须更改脚本 function 以接受 2 个文件。 Also change in your template...还要更改您的模板...
<form action="your_view_url" method="post">
to至
<form action="my_view" method="post">
Add my_view to your urls.py将 my_view 添加到您的 urls.py
forms.py edit these 2 lines forms.py 编辑这两行
Aws_SourceFile = forms.FileField(upload_to='your_directory_name/')
Aws_RefernceFile = forms.FileField(upload_to='your_directory_name/')
Views.py.. (my_view) # You need to pass the exact location of your files to the script Views.py.. (my_view) # 您需要将文件的确切位置传递给脚本
Aws_SourceFile = request.FILES.get('Aws_SourceFile')
Aws_RefernceFile =request.FILES.get('Aws_RefernceFile')
ASF_dir_file = os.path.join('your_directory_name', Aws_SourceFile.uploadfile.name)
ARF_dir_file = os.path.join('your_directory_name', Aws_RefernceFile.uploadfile.name)
output = script_function ( ASF_dir_file, ARF_dir_file )
def script_function(ASF_dir_file, ARF_dir_file):
output_asf = subprocess.check_call ( [ '\script_18122020.py' , ASF_dir_file] )
output_asr = subprocess.check_call ( [ '\script_18122020.py' , ARF_dir_file] )
return output_asf, output_asf
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.