2011-08-12 30 views
0

我正在嘗試將Channel API與我的Google App Engine django-nonrel項目一起使用。根據我當前的要求,我想將位於某個頁面的所有用戶列表(例如group_mainpage.html)發送給位於同一頁面上的所有其他用戶。換句話說,如果我們都在這個頁面上,我們都可以在這個頁面的角落裏看到我們的名字。一旦用戶從頁面移出,他們的名字應該從列表中刪除。但是我無法正確製作JSON並顯示它。直到現在我也做過這樣的,但它不工作:simplejson在Google App Engine頻道API中轉儲

group_mainpage.html

<html> 
    <head> 
    </head> 
    <body> 
     <div id="channel_api_params" style="display:none;" chat_token="{{chat_token}}" channel_id="{{channel_id}}"></div> 
     <div align="center"><font size="5" color="blue">Welcome To Group Main Page</font><br><br> 
     </div> 
     <div align="center"> 
      <form method="POST" action="/group_start/"> 
       <input type='submit' value="Start"> 
      </form> 
      <div id="mydiv"> 
      {% include 'user_list.html' %} 
      </div> 
     </div> 
     <script type="text/javascript" src="/media/jquery.js"></script> 
     <script type="text/javascript" src="/_ah/channel/jsapi"></script> 
     <script type="text/javascript"> 
     $(document).ready(function(){ 
      $(window).load(function(){ 
       var channel_id = $('#channel_api_params').attr('channel_id'); 
       $.ajax({ 
        url: '/valid_group_users/', 
        type: 'GET', 
        data:{ 
         'channel_id':channel_id, 
        }, 
        success: function(current_user){ 
        }, 
        complete: function(){ 
        } 
       }); 

       var chat_token = $('#channel_api_params').attr('chat_token'); 
       var channel = new goog.appengine.Channel(chat_token); 
       var socket = channel.open(); 
       socket.onopen = function(){ 
       }; 
       socket.onmessage = function(m){ 
        var data = $.parseJSON(m.data); 
        $('#mydiv').append(data['post_element']); 
       }; 
       socket.onerror = function(err){ 
        alert("Error => "+err.description); 
       }; 
       socket.onclose = function(){ 
        alert("channel closed"); 
       }; 

      }); 
     }); 
     </script> 
    </body> 
</html> 

views.py

def valid_group_users(request): 
    channel_id=request.GET['channel_id'] 

    group_initialise=Group_initialise() 
    group_initialise.channel_id=channel_id 
    group_initialise.user_name=request.user 
    group_initialise.save() 

    try: 
     data=Group_initialise.objects.all() 
    except: 
     pass 

#As per the suggestions of Kevin: 
user_list=[] 
for result in data: 
    user_list.append(result.user_name) 
    template_values={'user_list':user_list} 
temp_result={'post_element':render_to_response("user_list.html",template_values)} 

channel_msg=simplejson.dumps(temp_result) 

for result in data: 
    if result.user_name!=request.user: 
     channel.send_message(result.channel_id,channel_msg) 

user_list.html

{% for users in user_list %} 
    <div class="message"> 
     <span> 
      {{users}}: 
     </span> 
    </div> 
{% endfor %} 

編輯:

temp_result=str(temp_result) 
#To remove Http-Header/content-type copy string after 40 characters 
temp_result=temp_result[40:] 
#Replace colon attaching automatically at the end of every user_name 
temp_result=temp_result.replace(':','') 
channel_msg=simplejson.dumps(temp_result) 
+2

你說這不起作用,但是*發生了什麼?你有錯誤嗎?數據看起來不正確?更多細節會有所幫助。 – Herms

+0

Hello Herms,我得到的錯誤是'不是JSON可序列化' – SRC

回答

0

channel_msg=simplejson.dumps(outstr)應該用於發送JSON數組,而不是HTML。

試試這個:

outstr={'page_element':render_to_response("user_list.html",template_values)} 

,然後在你的JavaScript代碼:

$('#mydiv').append(data['page_element']); 

另外,我覺得你缺少一個 '+ =' 在結果循環:

for result in data: 
     user_list+=result.user_name 
+0

您好凱文,感謝您的答案。但它不工作。它給我錯誤「不是JSON可序列化」。 – SRC

0

經過一些試錯過程後,我設法找到了一種方法,而且似乎目前正在進行。我不知道這是否是最好的方法。我編輯了代碼以反映現在正在工作的更改。

+0

根據您的要求,另一種考慮的方法可能會給您更大的靈活性,這將是一個雙腿方案。即不是將html作爲Channel API消息的一部分發送,而是使用消息觸發元素的(javascript)刷新(有效地允許將views.py分成兩個處理程序 - 一個用於觸發事件,另一個用於呈現用戶列表元素)。 –

+0

@凱文:感謝凱文的建議。但我不知道我得到了你的建議。你有沒有小樣本代碼/鏈接的方法。再次感謝您的時間。 – SRC

+0

或者您可以更新我的給定摘錄。 – SRC