开发者

simplejson dumps in Google App Engine Channel API

I am trying to use the Channel API with my Google App Engine django-nonrel project. As per my current requirement I want to send all the users list who are at a certain page, say group_mainpage.html, to every other users who are on the same page. In other words, if we both are at this page, we both can see our names somewhere in the corner of this page. Once the users move from the page, their name should be removed from the list. But I am not able to make JSON of it properly and display it. Till now I have done like this but it's not working:

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 sugges开发者_开发百科tions 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 %}

EDIT:

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)


channel_msg=simplejson.dumps(outstr) should be used to send a JSON array, not HTML.

Try this:

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

and then in your JavaScript code:

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

Also, I think you are missing a '+=' in your result loop:

for result in data:
        user_list+=result.user_name


After some trial-error process, I have managed to find a way, and it seems to work for the moment. I don't know if it is the best approach. I have edited the code to reflect my changes which is now working.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜