我正在使用 ajax 对来自搜索结果的数据进行排序。
现在我想知道是否可以只渲染 html 的一部分,以便我可以这样加载:
$('#result').html(' ').load('/sort/?sortid=' + sortid);
我正在这样做,但我得到了整个 html 页面作为响应,并且它将整个 html 页面附加到现有页面,这很糟糕。
这是我的观点.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))
我怎样才能只渲染那个<div id="result"> </div>
从我的角度来看功能?或者我在这里做错了什么?
据我了解,如果您收到 ajax 请求,您希望以不同的方式处理相同的视图。
我建议分开你的result-page.html
分成两个模板,一个仅包含您想要的 div,另一个包含其他所有内容并包含另一个模板(请参阅django 的 include 标签 https://docs.djangoproject.com/en/dev/ref/templates/builtins/#include).
在您看来,您可以执行以下操作:
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))
结果页.html:
<html>
<div> blah blah</div>
<div id="results">
{% include "partial-results.html" %}
</div>
<div> some more stuff </div>
</html>
部分结果.html:
{% for location in locs %}
{{ location }}
{% endfor %}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)