Answer the question
In order to leave comments, you need to log in
Append("some jquery functionon") not working in jquery when getting them with ajax?
Good day. There was a problem updating content dynamically. There is django-server and ajax requests using setInterval() pull data from the server. So in django-templates I prepared a template with jquery code and when sending from the server to ajax, I render using .append() or .html(). Everything works fine on page refresh. But it seems to me that what I supposedly srendrilled is how the text falls into the dom, but JQ does not execute it because it is not a "console" type, but it only executes the things that are in front of it. I understand correctly?
Okay, here's the actual django code:
from django_ajax.decorators import ajax
from message.models import Message
from django.utils import timezone
@ajax
def index(request):
context={
"friends":[],
"messages":[],
}
index=0
if Message.objects.unread_messages_count(request.user)!=0:
for message in Message.objects.filter(to_user=request.user,viewed__isnull=True,messaged__isnull=True):
index+=1
context["messages"].append({
"message":message.message[:255],
"user":message.from_user.get_full_name,
"duration":index*2200+6000
})
message.messaged=timezone.now()
message.save()
return render(request, 'message.html',context)
<script src="{% static 'scripts/js/jquery-latest.js' %}"></script>
<script type="text/javascript" src="{% static 'django_ajax/js/jquery.ajax.min.js' %}"></script>
<script src="{% static 'scripts/js/jquery.growl.js' %}"></script>
<link rel="stylesheet" href="{% static 'scripts/css/jquery.growl.css'%}">
<script>
function ajax_updates() {
ajaxGet("{% url 'ajax_updates' %}",function(content){
$('#notifications').append(content);
})
}
setInterval(ajax_updates, 5000);
ajax_updates();
</script>
<script id="notifications"></script>
{% for message in messages %}
$.growl({ title: "{{message.user}}", message: "{{ message.message }}",duration: {{message.duration}}});
{% endfor %}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question