Refresh
element generated by a django template

前端 未结 2 1121
孤街浪徒
孤街浪徒 2020-12-01 09:33

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

2条回答
  •  执笔经年
    2020-12-01 10:19

    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 %}
        {{ other_object.title }}
         
    {% endfor %}
    

    Best regards!

提交回复
热议问题