Rendering JSON objects using a Django template after an Ajax call

前端 未结 8 1791
南笙
南笙 2020-12-02 03:59

I\'ve been trying to understand what\'s the optimal way to do Ajax in Django. By reading stuff here and there I gathered that the common process is:

  1. formul

8条回答
  •  粉色の甜心
    2020-12-02 04:36

    Here is how I use the same template for traditional rendering and Ajax-response rendering.

    Template:

    {% include "admin/app/model/subtemplate.html" %}

    Included template (aka: subtemplate):

    {% if results %} {% for c in results %} ..... {% endfor %} {% else %}

    The Ajax-view:

    @login_required
    @render_to('admin/app/model/subtemplate.html')#annoying-decorator
    def ajax_view(request):
        .....
    
        return { 
            "results":Model.objects.all(),
        }      
    

    Of course you can use render_to_response. But I like those annoying decorators :D

提交回复
热议问题