How do I refresh a certain element within a django template?
Example:
{% if object.some_m2m_field.all %}
The stuff I want to refresh is be
You can have a look at eg. this Ajax with Django tutorial. Anyways as mentioned above you can always use django's template engine, no matter if the view is called in a normal or an ajax request! If you have to use ajax with django more frequently it makes sense to have a look at something like dajax, which is an ajax library for django (have a look at the tutorials there).
You could use an async request to fill the div element. The async request is answered by django using the template engine.
In this case you would have to outsource the template code of the div element into a seperate template file.
UPDATED WITH EXAMPLE:
Javascript:
For refreshing the view asynchronously, use JQuery for example:
$.ajax({
url: '{% url myview %}',
success: function(data) {
$('#the-div-that-should-be-refreshed').html(data);
}
});
Async View:
def myview(request):
object = ...
return render_to_response('my_template.html', { 'object': object })
Template:
{% for other_object in object.some_m2m_field.all %}
<a href="www.example.com">{{ other_object.title }}</a>
{% endfor %}
Best regards!