如何使用Django仅使用数据呈现HTML的一部分 [英] how to render only part of html with data using django

查看:110
本文介绍了如何使用Django仅使用数据呈现HTML的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ajax对来自搜索结果的数据进行排序。



现在,我想知道是否有可能只渲染html的一部分,以便我可以这样加载:



$('#result')。html('& nbsp;')。load('/ sort /?sortid ='+ sortid);

我正在这样做,但是我得到整个html页面作为响应,并将整个html页面附加到现有页面上,这很糟糕。



这是我的views.py

  def sort(request) :
sortid = request.GET.get('sortid')
评分= Bewertung.objects.order_by(sortid)
location = Location.objects.filter(locations_bewertung__in = ratings)
return render_to_response('result-page.html',{'locs':locations},context_instance = RequestContext(request))

如何仅渲染< div id = result> < / div> 来自我的视图函数?还是我在这里做错了什么?

解决方案

据我了解,如果收到ajax请求,您希望以不同的方式对待同一视图。
我建议将您的 result-page.html 分成两个模板,一个仅包含所需的div,一个包含其他所有内容并包括其他模板(请参见 django的包含标签)。



在您看来,您可以执行以下操作:

  def sort(request):
sortid = request.GET.get('sortid')
评分= Bewertung.objects.order_by(sortid)
位置= Location.objects.filter(locations_bewertung__in =等级)
如果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>等等等等// div;
< div id = results>
{%include partial-results.html%}
< / div>
< div>其他一些< / div>
< / html>

partial-results.html:

  {%为位置的位置}} 
{{location}}
{%endfor%}


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:

$('#result').html('&nbsp;').load('/sort/?sortid=' + sortid);

I am doing this but I am getting the whole html page as response and it is appending the whole html page to the existing page which is terrible.

this is my views.py

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

how can I render only that <div id="result"> </div> from my view function? or what am I doing here wrong?

解决方案

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 %}

这篇关于如何使用Django仅使用数据呈现HTML的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆