我的问题是如何在django web应用程序的视图中实现脚本
我有一个.py
脚本,我想使用用户提供的数据configfile
、modelfile
和选项作为命令行参数在这个django web应用程序中运行。然后,输出是一个具有不同分数的列表,该列表将显示在html中(不是我需要帮助的部分)
因此,我有两个表单,一个用于上传ML模型,另一个(我需要帮助的表单)使用用户提供的数据运行上传的模型:
class Meta:
model = MlModel
fields = [
'modelname',
'configfile',
'modelfile',
'choice',
]
class TestForm(forms.Form):
testdata = forms.FileField()
model = forms.ModelChoiceField(queryset=MlModel.objects.all())
我的观点如下,其中测试视图是测试发生的地方:
if request.method == 'POST':
form = UploadForm(request.POST, request.FILES)
if form.is_valid():
form.save()
return HttpResponseRedirect('/mltesting/')
else:
form = UploadForm()
context = {'form':form}
return render(request,'mltesting/upload.html', context=context)
def test_model(request):
submitbutton = request.POST.get("submit")
testdata = ''
modfile = ''
confile = ''
choice = ''
form = TestForm(request.POST, request.FILES)
if form.is_valid():
testdata = request.FILES['testdata']
model = form.cleaned_data.get('model')
model = MlModel.objects.get(modelname=model)
modfile = model.modelfile.path
confile = model.configfile.path
choice = model.choice
else:
form = TestForm()
context = {'form': form, 'testdata': testdata, 'submitbutton': submitbutton, 'modelfile':modfile, 'configfile': confile,'choice': choice}
return render(request,'mltesting/test.html', context=context)
这是HTML模板,测试的输出应该是:
{% block content %}
<p>Compound Protein Interaction Tool</p>
<form action="" method="POST" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_table }}
<input type="Submit" name="submit" value="Submit"/>
</form>
{% if submitbutton == "Submit" %}
{% endif %}
{% endblock %}
总的来说,我对Django还是一个新手,所以我为我的问题(可能)的糟糕表述道歉,但非常感谢所有的帮助
你可以从这个solution中找到答案
在同一个目录中创建一个文件夹,在那里添加py文件,然后在view.py中导入所需的模块
像调用任何自定义模块一样调用函数
***编辑** 调用此answer中所述的脚本
相关问题 更多 >
编程相关推荐