如何使用视图中的参数在Django web应用程序中实现python脚本

2024-05-14 16:56:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我的问题是如何在django web应用程序的视图中实现脚本

我有一个.py脚本,我想使用用户提供的数据configfilemodelfile和选项作为命令行参数在这个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还是一个新手,所以我为我的问题(可能)的糟糕表述道歉,但非常感谢所有的帮助


Tags: formmodelifrequesthtmlcontextformspost
1条回答
网友
1楼 · 发布于 2024-05-14 16:56:07

你可以从这个solution中找到答案

在同一个目录中创建一个文件夹,在那里添加py文件,然后在view.py中导入所需的模块

像调用任何自定义模块一样调用函数

***编辑** 调用此answer中所述的脚本

def test_model(request):

    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()

    #########################################
    ###########Here you run your script
    ###########Any variable from the script will be loaded here
    exec(open("./path/toYour/script.py").read(), globals())


    #Add the information you want from the script to the context
    context = {'someVariableFromTheScript':someVariableFromTheScript, 'form': form, 'testdata': testdata, 'submitbutton': submitbutton, 'modelfile':modfile, 'configfile': confile,'choice': choice}
    return render(request,'mltesting/test.html', context=context)

相关问题 更多 >

    热门问题