I am deploying a website using Django. There is an application called 
'forum', supporting a discussion forum on my website.

The url of the discussion forum is 'XXX.com/forum/roomY'. I want to refresh 
a div id = ''chat'', which includes a message list, on this page when users 
click a refresh button. I want to use AJAX to do that.


But I find that I could not call the function updatestatementlist(request) 
to retrieve the updated message list so it can be passed to the on this 
page.


*/forum/views.py def updatestatementlist(request):*

log.debug("call statementlist function")
statements = Statement.objects.filter(discussion=discussion)return 
render(request, 'forum/statementlist.html', {
    'statements': statements
    })


I cannot see the log info so I think by clicking the button I fail to call 
this function.


The main html page of the discussion forum is */forum/discussion.html*, 
which is under the template folder. I extract the html code within the div 
id = "chat" to a separate html */forum/statementlist.html*as suggested here 
<https://stackoverflow.com/questions/3583534/refresh-div-element-generated-by-a-django-template>
 and 
several other SO posts.


*/forum/discussion.html*

<button id = "Refresh"> Refresh </button><div id="chat">
{% include 'forum/statementlist.html' %}</div>

*/forum/statementlist.html*

{% recursetree statements %}{% endrecursetree %}

*forum.js*

//When users click the refresh button
    $("#Refresh").on("click", function(event){
    alert("Refresh clicked")
    $.ajax({
        url: '',
        type: "GET",
        success: function(data) {
            alert("success")
            var html = $(data).filter('#chat').html();
            $('#chat').html(html);
        }
    });});


I also tried a few other url in this AJAX request: {% url 
updatestatementlist %}, {% url 'updatestatementlist' %}. But then I think 
it should be set to empty because I don't want to be redirected to another 
url. The discussion forum has a url of 'XXX.com/forum/roomY', by clicking 
the refresh button on this page I only want to refresh the div and fetch an 
updated statement list from the server.


BTW, I can see the two alerts after I click the button.


*/forum/urls.py*

urlpatterns = [...
url(r'^(?P<label>[\w-]{,50})/$', views.discussion_forum, 
name='discussion_forum'),
url(r'^(?P<label>[\w-]{,50})/$', views.statementlist, name='statementlist'),

]


/forum/views.py def discussion_forum() is used to load all the information 
when the user first arrives at this forum.


I guess my problem might be that 1) the AJAX is wrong; 2) the url is wrong 
so that the updatestatementlist() can not be called.


Can anyone help me with that? Thank you so much.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/e8cf17fa-a7f7-4002-86dc-4b2460a3e140%40googlegroups.com.

Reply via email to