how to render only part of html with data using django

前端 未结 1 1471
鱼传尺愫
鱼传尺愫 2021-02-01 09:43

I am using ajax to sort the data which came from search results.

Now I am wondering whether it is possible to render just some part of html so that i can load this way:

相关标签:
1条回答
  • 2021-02-01 10:34

    From what I understand you want to treat the same view in a different way if you receive an ajax request. I would suggest splitting your result-page.html into two templates, one that contains only the div that you want, and one that contains everything else and includes the other template (see django's include tag).

    In your view then you can do something like the following:

    def sort(request):
        sortid = request.GET.get('sortid')
        ratings = Bewertung.objects.order_by(sortid)
        locations = Location.objects.filter(locations_bewertung__in=ratings)
        if request.is_ajax():
            template = 'partial-results.html'
        else:
            template = 'result-page.html'
        return render_to_response(template,   {'locs':locations},context_instance=RequestContext(request))
    

    results-page.html:

    <html>
       <div> blah blah</div>
       <div id="results">
           {% include "partial-results.html" %}
       </div>
       <div> some more stuff </div>
    </html>
    

    partial-results.html:

    {% for location in locs %}
        {{ location }}
    {% endfor %}
    
    0 讨论(0)
提交回复
热议问题