2013-02-13 36 views
0

我正在運行一個Django的Web應用程序,當我第一次提交表單失敗,我需要刷新並再次提交它發送數據,或做後退在網絡瀏覽器上。 我使用了自動色彩代碼JavaScript程序(editarea)在一個textarea表單提交失敗的第一次並刷新頁面時工作

這裏是我的HTML文件:

<html> 
<head> 
    <title>Test</title> 
    <script language="javascript" type="text/javascript" src="/static/edit_area/edit_area_full.js"></script> 
    <script type="text/javascript"> 
     function load() { 
      var combo = document.getElementById('selSeaShells1').value; 
      editAreaLoader.init({ 
       id: "textarea_1"  // textarea id 
        , syntax: combo    // syntax to be uses for highgliting 
        , start_highlight: true  // to display with highlight mode on start-up 
      }); 
     } 
    </script> 
</head> 
<body onload="load();"> 
    <form id="codeid" method="post" enctype="application/x-www-form-urlencoded" name="code" action="DestinationAddress/function"> 
    <h3>Choose a language</h3> 
    <select name="sellang1" id="selSeaShells1" onchange="load();"> 
     <option value="python">Python</option> 
     <option value="perl">Perl</option> 
     <option value="sql">SQL</option> 
     <option value="cpp">C++</option> 
     <option value="c">C</option> 
     <option value="java">Java</option> 
     <option value="css">Css</option> 
    </select> 
    <br> 
    <textarea id="textarea_1" name="content" cols="80" rows="15" type="text"></textarea> 
    <input id="thebutton" type="button" value="Submit" onclick="document.forms.codeid.submit();" /> 
    </form> 
</body> 
</html> 

這裏的views.py:

def function(request): 

encoded_data = urllib.urlencode(request.POST) 

url=urllib2.urlopen('http://webappAddress:8000/function/?' + encoded_data) 

tml= url.read() 

return HttpResponse(tml) 
+1

當它失敗時,究竟發生了什麼?是否有錯誤訊息?你有沒有嘗試過與JavaScript控制檯打開? – 2013-02-13 21:08:50

+0

您的視圖是否實際調用同一網站內的網址?如果是這樣,*爲什麼*? – 2013-02-13 21:13:58

回答

1

你的JavaScript失敗。
var combo = document.getElementById('selSeaShells1')。value;
組合將會是未定義的,因爲您在頁面完成加載之前調用load()。
我會使用jquery和做到這一點:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> //download and get a local copy of jquery don't use googles.***<script> 

$(document).ready(function() { 
    load(); 
}); 

function load() 
    {  
     var combo = $('#selSeaShells1 option:selected).val(); 

      editAreaLoader.init({ 
      id : "textarea_1"  // textarea id 
      ,syntax: combo    // syntax to be uses for highgliting 
      ,start_highlight: true  // to display with highlight mode on start-up 
      }); 


    } 

</script> 

刪除的onload = 「負載();」在身體標記
這將在頁面加載後得到選擇。