2017-03-24 30 views
0

私はコンソール出力をHTMLに表示するためにDjangoフレームワークを使用しています。このコマンドを実行するには、check_outputsubprocessモジュールをPythonで使用しています。 HTML入力フォームからの入力を受け取ります。問題は、私がファイル内のデフォルト値outputである "None"しかHTMLページに表示されないということです。 以下は、ビューファイルとHTMLファイルのコードです。私はこれの初心者ですので、私はあなたの援助に感謝します。Djangoでコンソール出力をHTMLに表示するにはどうすればよいですか?

Views.py

from django.shortcuts import render 
from django.shortcuts import redirect 
from .forms import command_form 
import subprocess as sp 


# Create your views here. 

def welcome_page(request): 
    output="" 
    if request.method == "POST": 
     myform = command_form(request.POST) 
     if (myform.is_valid()): 
      execute_command = myform.cleaned_data['cmd_string'] 
      output = sp.check_output(execute_command, shell=True) 
     else: 
      myform = command_form() 
     return render(request, 'ovs/welcome.html', {'output': output}) 
    else: 
     return render(request, 'ovs/welcome.html', {}) 

のwelcome.html

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>welcome to ovs GUI</title> 
</head> 
<body> 
    <h3>Choose the option:</h3> 
    <form method="POST">{% csrf_token %} 
     Enter the command: <input type="text" name="cmd_string" id="cmd_string"/> 
     <input type="submit" value="Run"/> 
    </form> 
    <h3>{{ output }}</h3> 
</body> 
</html> 

フォーム

from django import forms 


class command_form(forms.Form): 
    command = forms.CharField(max_length=200) 
+0

あなたはそれがうまくいかないことを説明できますか?あなたの問題は何ですか? –

+0

私の問題は、データがコンソール出力が表示されないということです。 HTMLページには常に "output"のデフォルト値が与えられます。それは、たとえそれがpostメソッドからデータを解析できるのであれば私に懐疑的なものになります。 –

+1

@AnandTyagi。以下の回答のいずれかがあなたのために働いたのですか? –

答えて

1

HTMLにフォームフィールドを正しく表示していません。 command_formフォームが作成されており、これを悪用することはありません。ただし、のように、Python classの場合はキャメルケース名を使用する必要があります。あなたのHTML内

、この書き込み:

def welcome_page(request): 
    output = "" 
    # Initialize the form. At this point you have an unbound/invalid form 
    myform = command_form() # better write it as CommandForm 

    if request.method == "POST": 
     myform = command_form(request.POST) 
     if myform.is_valid(): 
      # execute_command variable, should now contain the command typed by the user in the text box 
      execute_command = myform.cleaned_data['command'] 
      try: 
       # If the return code is non-zero, CalledProcessError will be raised 
       output = sp.check_output(execute_command, shell=True) 
      except sp.CalledProcessError: 
       exit_code, error_msg = output.returncode, output.output 
     else: 
      # Do something when the form is not valid. Maybe add a message or something, or not implement else clause at all. 
    return render(request, 'ovs/welcome.html', locals()) 

警告:

<form method="POST">{% csrf_token %} 
    Enter the command: {{ myform }} 
    <input type="submit" name="submit_cmd" value="Run" /> 
</form> 
{% if output %}<h3>{{ output }}</h3>{% endif %} 
{% if exit_code %}<h3>Your command returned an error: {{ error_msg }}</h3>{% endif %} 

{{ my_form }}は、このようなあなたのwelcome_pageビューを書き、今<input type="text" ...>

に、自動的に、拡大していきます! docsとして:

shell=Trueを使用するとセキュリティ上の危険が生じる可能性があります。

+0

ありがとうございます。しかし、あなたが言ったようにHTMLを変更すると入力ボックスが表示されません。私は何をすべきか? –

+0

まあまあ。私の答えを更新しました。 '{{my_form}}'の代わりに '{{myform}}'に変更されました。 –

0

あなたはRESTフレームワークを使用することができますレスポンスを返すので、HTMLで扱うことを心配する必要はありません。 rest_frameworkをインストールして、これを実行してください:

from rest_framework.response import Response 

return Response(data) 
+0

しかし、私は私のHTMLデータの下に出力を表示する必要があります。すなわち、コマンドラインをテキストとして受け取る形式の下に表示したい。 –

1
views.py 

from django.shortcuts import render 
from django.shortcuts import redirect 
from test.forms import CommadForm 
import subprocess as sp 

def welcome_page(request): 
    if request.method == "POST": 
     myform = CommadForm(request.POST) 
     if myform.is_valid(): 
      execute_command = myform.cleaned_data['command'] 
      try: 
       output = sp.check_output(execute_command, shell=True) 
      except sp.CalledProcessError: 
       output = 'No such command' 
     else: 
      myform = CommadForm() 
     return render(request, 'ovs/welcome.html', {'output': output}) 
    else: 
     return render(request, 'ovs/welcome.html') 

forms.py 

class CommadForm(forms.Form): 
    command = forms.CharField(max_length=200) 
+0

htmlページはあなたが使ったものと同じになります –

関連する問題