2014-01-10 7 views
2

일부 파이썬 스크립트를 실행하기 위해 웹 인터페이스를 구축하려고합니다. 이러한 스크립트 중 하나를 사용하여 네트워크 장치에 로그인하고 일부 명령을 실행 한 다음이를 Excel 파일에 저장합니다.Django는 양식 필드의 변수를 사용하여 python 스크립트를 실행합니다.

내가 올바른 방법인지 확신 할 수는 없지만 스크립트를 관리 명령으로 만들었습니다.

call_command()를 사용하여 뷰에서 스크립트를 실행할 수는 있지만 스크립트로 양식 필드를 변수로 전달하는 방법에 대해 난처한 입장입니다.

여기 내가 관련 부분이라고 생각하는 것입니다.

form.py

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...> 

views.py

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

baseline.py

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

별도의 스레드에서보기로 스크립트를 실행하고 그에 따라 해당 응답을 처리 할 수있는 다른보기에 결과를 게시하도록하는 것이 좋습니다. 이 모든 것이 비동기식으로 이루어 지므로 웹 앱이 덤프되지 않습니다 – arnm

+0

@Alexei Nunez이 예제가 있습니까? 앞으로는 작업 대기열에 셀러리를 사용할 계획입니다. 양식 필드를 스크립트의 변수로 전달하는 것에 대한 아이디어가 있습니까? –

+0

아니요 예제가 없지만 작동하지 않는 이유를 모르겠습니다. 나는 그것이 최선의 제안이라고 말하는 것이 아니지만 작동 할 것이다. python에서 스크립트를 실행하는 방법 : http://stackoverflow.com/questions/3781851/run-a-python-script-from-another-python-script-passing-in-args. 다음과 같이 게시물을 작성하십시오. http://docs.python-requests.org/en/latest/ – arnm

답변

1

앞으로 도움이 될 수 있으므로 여기에 추가 할 예정입니다.

는 인수

args = ['/path/tp/script/get_baseline.py', username, password, enable, user_email, device] 

실행 목록을 만들 뷰

from nettools import tasks 

에 주 프로젝트 디렉토리에 task.py 파일에

@app.task(name='tasks.get_baseline') 
def get_baseline(args): 
    result = call(args) 

가져 오기 작업을 작업을 추가 작업

tasks.get_baseline.delay(args) 
관련 문제