将python脚本与django集成 [英] integrating python scripts with django

查看:317
本文介绍了将python脚本与django集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在python中编写了一些测试脚本,它为一个paritcular应用程序调用一些apis,并将结果输出到一个数据库,以便我可以使用jasper来报告结果。目前,脚本使用python解释器运行,即双击python文件,该文件具有脚本中修改的一些参数和变量,然后启动测试。我想转向更友好的界面,因此其他人可以使用这些测试脚本,而无需修改python代码。所以我正在考虑使用django和创建一个有复选框的网页,如果勾选这个复选框,那么它会执行那个特定的python测试脚本或者一个文本框,把这个值传递给给定的变量,例如。我有几个问题可以如何实现。



1 - 我需要将python代码实现到django源代码中,还是可以调用python脚本从django提供的网页?



2 - 如果我要从网页运行,我如何确保如果网页被关闭,那么测试将在后台继续。



3 - 有没有办法将测试用例的状态输出到网页,如果网页已关闭,则如果网页被重新打开,状态是可用的?



非常感谢 - oli

解决方案

如果你有一个python函数,你可以从Django django视图调用,也可以使用表单作为参数输入。如果您有长时间运行的进程,您可能需要从这里考虑一个提示:如何从Django视图开始长时间运行的过程?

  mytests import testfunc 

def test_parameter_view(request):
如果request.method =='POST':#如果表单已经提交...
form = ParameterForm(request.POST)
如果form.is_valid():
testfunc(form.cleaned_data ['parameter'])#< - 这里实际测试发生
return HttpResponseRedirect(reverse(test_result))#重定向POST
else:
form = ParameterForm()

返回render_to_response('test.html',{
'form':form,
})

在您的 test_result 视图中,您可以访问从数据库测试结果值。



如果用户关闭浏览器不影响已经开始的服务器进程。而且由于您将结果写入数据库,因此它们将持续存在,并可在测试完成后随时访问。


I have written some test scripts in python which call some apis for a paritcular application and output the results to a database so I can use jasper to report on the results. Currently the scripts are run using a python interpreter, ie double click on the python file that has some parameters and variables modifed within the script that then initiates the tests. I would like to move towards a more user friendly interface so other people can use these test scripts without having to modify the python code. So I am thinking about using django and creating a web page that has check boxes, and if the check box is ticked, then it will execute that particular python test script or a text box that will pass the values to a given variable, for example. I have a few questions around how this might be achieved.

1 - would I need to implement the python code in to the django source code or can I call the python script from the web page served up by django?

2 - If I were to run this from a web page, how could I ensure that if the web page was closed, that the test would continue in the background.

3 - Is there a way to output the status of the test case to a web page and if the web page was closed, for the status to be availble if the web page was reopened?

Many thanks - oli

解决方案

If you have a python function you can call from a Django django view maybe with a form as parameter input. If you have long running processes you might want to consider a tip from here: How to start a long-running process from a Django view?

from mytests import testfunc

def test_parameter_view(request):
    if request.method == 'POST': # If the form has been submitted...
        form = ParameterForm(request.POST)
        if form.is_valid():
            testfunc(form.cleaned_data['parameter'])   # <-- Here the actual testing happens
            return HttpResponseRedirect(reverse(test_result)) # Redirect after POST
    else:
        form = ParameterForm()

    return render_to_response('test.html', {
        'form': form,
    })

In your test_result view you can access the test result values from the database.

If the user closes the browser or not doesn't affect server processes that already have been started. And since you write your results to the database they are persistent and can be accessed any time after the test has finished.

这篇关于将python脚本与django集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆