Rendering JSON objects using a Django template after an Ajax call

前端 未结 8 1747
南笙
南笙 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:

    <div  id="sortable">
    
    {% include "admin/app/model/subtemplate.html" %}
    </div>
    

    Included template (aka: subtemplate):

    <div id="results_listing">
    {% 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

    0 讨论(0)
  • 2020-12-02 04:40

    You can use jquery.load() or similar, generating the HTML on the server and loading it into the DOM with JavaScript. I think someone has called this AJAH.

    0 讨论(0)
提交回复
热议问题