我正在尝试构建一个Web界面来执行一些python脚本。一个这样的脚本用于登录网络设备并运行一些命令,然后将它们保存到excel文件中。
我不确定这是否是正确的方法,但我已将该脚本变为管理命令。
我可以通过使用call_command()从视图中运行脚本,但我对如何将表单字段作为变量传递给脚本感到困惑。
以下是我认为相关的部分;
class BaseLineForm(forms.Form):
username = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Required', 'size': 20}), error_messages={'required': 'Required'})
cec_pass = forms.CharField(widget=forms.PasswordInput(attrs={'placeholder': 'Required', 'size': 20}), error_messages={'required': 'Required'})
enable_pass = forms.CharField(widget=forms.PasswordInput(attrs={'placeholder': 'Required', 'size': 20}), error_messages={'required': 'Required'})
<additional form fields snipped...>
from baseline.forms import BaseLineForm
from django.core.management import call_command
def baseline(request):
if request.method == 'POST':
form = BaseLineForm(request.POST)
if form.is_valid():
username = form.cleaned_data['username']
password = form.cleaned_data['cec_pass']
enable = form.cleaned_data['enable_pass']
call_command('baseline')
from django.core.management.base import BaseCommand, CommandError
class Command(BaseCommand):
args = ''
help = ''
def handle(self, *args, **options):
<I have tried to import the view but I get an error cannot import name views>
from baseline import views <if I run this from the shell it imports fine>
<rest of script here>
环境; Centos 6.4,virtualenv,django 1.6,python 2.6
我真的很难过,所以请提前感谢任何想要提供帮助的人。
答案 0 :(得分:1)
这可能会对将来有所帮助,所以我在这里添加。
@app.task(name='tasks.get_baseline')
def get_baseline(args):
result = call(args)
from nettools import tasks
args = ['/path/tp/script/get_baseline.py', username, password, enable, user_email, device]
tasks.get_baseline.delay(args)