2016-01-14 64 views
0

我試圖編寫一個代碼,當用戶想要檢查狀態時服務器(AWS實例)的狀態可以返回到瀏覽器。有時它可能需要一段時間對於服務器啓動,所以我希望每次用戶單擊按鈕時刷新狀態的值(再次運行Django代碼以獲取新值)。
例子:
[檢查狀態](按鈕)
Web服務器,測試運行Ajax從按鈕點擊獲取Django 1.9代碼的新值

截至目前(每個按鈕被按下時這條線被刷新),我不知道怎麼去view.py再次運行並返回要使用的html頁面的新值。

這是我view.py

def indcheckstatus(request, question_id, check_id): 
    inst() #runs an update on the instance (instance2[0].update() etc.) 
    text1 = 'WebServer-Test %s' % instance1[0].state 
    text2 = 'Gluster %s' % instance2[0].state 
    text3 = 'Gluster2 %s' % instance3[0].state 
    text4 = 'Database %s' % instance4[0].state 
    txt1 = 'WebServer-Test' 
    txt2 = 'Gluster' 
    txt3 = 'Gluster2' 
    txt4 = 'Database-Test' 
    if check_id == '1': 
     return render(request, 'awsservers/indcheckstatus.html', {'indstatus': text1, 'indserver':txt1}) 
    elif check_id == '2': 
     return render(request, 'awsservers/indcheckstatus.html', {'indstatus': text2, 'indserver':txt2}) 
    elif check_id == '3': 
     return render(request, 'awsservers/indcheckstatus.html', {'indstatus': text3, 'indserver':txt3}) 
    elif check_id == '4': 
     return render(request, 'awsservers/indcheckstatus.html', {'indstatus': text4, 'indserver':txt4}) 

這是我的Ajax代碼+ HTML代碼

<html> 
<body> 
<title>Server Management</title> 
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script> 
<script type="text/javascript"> 
$(document).ready(function() { 
    $("#state").hide() 
    var clicking = 1 
    $("#button").click(function() { 
     $.ajax({ 
      url : '', 
      success : function(response) { 
       clicking++ 
       $('#result').html($('#state').html() + clicking); 
       } 
      }); 
     return false; 
    }); 
}); 

</script> 

<noscript> 
    Some features require Javascript enabled 
</noscript> 

<form method="post"> 
    {%csrf_token%} 
    <input id="button" type="button" value="Check Status"></input> 
</form> 

<div id="result"> 
{{ indserver }} 
</div> 

<div id="state"> 
{{ indstatus }} 
</div> 

<h2>Status of {{ indserver }}</h2> 
<p>{{ indstatus }}</p> 

<a href="{% url 'awsservers:index' %}">Go Back</a> 

</body> 
</html> 

整點是,我不希望有整頁刷新加載新的值,但我希望刷新的值,每當按鈕被點擊。

我對Django非常陌生,特別是Ajax,所以請原諒我,如果我的代碼不是很「適當」:)我非常感謝幫助!請讓我知道是否有更好的方法來加載來自Django的新值!

回答

0

所以我找到了解決辦法。

這是我更新views.py

def indcheckstatus(request, question_id, check_id): 
    inst() // does an update of all instances 
    text1 = 'WebServer-Test %s' % instance1[0].state 
    text2 = 'Gluster %s' % instance2[0].state 
    text3 = 'Gluster2 %s' % instance3[0].state 
    text4 = 'Database-Test %s' % instance4[0].state 
    txt1 = 'WebServer-Test' 
    txt2 = 'Gluster' 
    txt3 = 'Gluster2' 
    txt4 = 'Database-Test' 
    if request.is_ajax(): // what to do if ajax code calls function 
     if check_id == '1': 
      return HttpResponse(text1) // return updated status back to html 
     elif check_id == '2': 
      return HttpResponse(text2) 
     elif check_id == '3': 
      return HttpResponse(text3) 
     elif check_id == '4': 
      return HttpResponse(text4) 
    else: 
     if check_id == '1': 
      return render(request, 'awsservers/indcheckstatus.html', {'indstatus': text1, 'indserver':txt1}) 
     elif check_id == '2': 
      return render(request, 'awsservers/indcheckstatus.html', {'indstatus': text2, 'indserver':txt2}) 
     elif check_id == '3': 
      return render(request, 'awsservers/indcheckstatus.html', {'indstatus': text3, 'indserver':txt3}) 
     elif check_id == '4': 
      return render(request, 'awsservers/indcheckstatus.html', {'indstatus': text4, 'indserver':txt4}) 

這是我的HTML + AJAX代碼:在我的代碼

<html> 
<body> 
<title>Server Management</title> 
<p>{{ indserver }} is starting up. </p> 

<p>Click the button below to get the status of the server</p> 
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script> 
<script type="text/javascript"> 
$(document).ready(function() { 
    $("#state").hide() 
    $("#start_id").hide() 
    $("#button").click(function() { 
     if ($("#start_id:contains('1')").length) { // to check which server was picked 
      $.ajax({ 
       type: "get", // requesting updated values from django 
       url : '/menu/2/1/status-ind', // determines which function is called 
       success : function(response) { 
        ($('#result').html(response)); 
       } // response is basically "[ServerName ServerState]" form (seen from code above) 
      }); 
     } 
     else if ($("#start_id:contains('2')").length) { 
     ...... // do code 
     } 
     return false; 
    }); 
}); 

</script> 
<noscript> 
    Some features require Javascript enabled 
</noscript> 
<form method="post"> 
    {%csrf_token%} 
    <input id="button" type="button" value="Check Status"></input> 
</form> 

<div id="result"> 
{{ indserver }} 
</div> 

<div id="state"> 
{{ indstatus }} 
</div> 

<div id="start_id"> 
{{ start_id }} 
</div> 
<br> 
<a href="{% url 'awsservers:index' %}">Go Back</a> 
</body> 
</html> 

我添加註釋來說明我所做的更改。我意識到必須編寫與獲取更新值所需的函數關聯的url,然後在我的views.py上寫入正確的響應以用於ajax代碼。它現在完美運行!