我一直想知道如何开发一个 django 视图,它支持通过 json 加载对象,并且对于阻塞 js 的客户端,它应该支持非 js 后备 - 使用像 ObjectName.objects.filter() 这样的查询集
对于支持 json 动态数据加载以及不使用 json 获取对象的此类视图,最好的方法是什么?或者只是我的方法是完全错误的,我应该获得更多关于如何编写像http://www.usatoday.com这样的 ajax 动态网站的信息
def index(request):
q = Page.objects.all()
return TemplateResponse(request, "orders/order_index.html",
{'object_list': q,},)
如果您需要与我的问题相关的任何其他数据,请直接问我。
更新 1 - 示例:
索引呈现您进入网站时看到的初始页面 (example.com)
def index(request):
q = Entry.objects.all().order_by('-id')
return TemplateResponse(request, 'index.html', {'object_list': q, })
def detail(request, slug):
q = get_object_or_404(Entry.objects.all(), slug=slug)
return TemplateResponse(request, 'blog/templates/blog_detail.html', {'object': q, })
索引.html
{% extends "base.html" %}
{% load webdesign %}
{% load markup %}
{% load humanize %}
{% load endless %}
{% load i18n %}
{% block content %}
{% paginate object_list %}
{% for object in object_list %}
<article>
<h2>{{ object.title }}</h2>
<div>
</div>
<hr>
<p>
{{ object.get_short_text|markdown }}
</p>
<div class="clearfix"></div>
<div class="row">
<div class="large-6 columns">
<span class="metadata"><i class="foundicon-clock"></i> {{ object.date_added|date:"j E Y" }} ({{ object.date_added|naturaltime }})</span>
</div>
</div>
<a class="button expand read_more" href="{{ object.get_absolute_url }}">{% trans "Read more" %}</a>
<hr>
</article>
{% endfor %}
{% show_pages %}
{% endblock %}
non ajax
[16/Mar/2013 15:16:52] "GET / HTTP/1.1" 200 2275
[16/Mar/2013 15:16:52] "GET /site_media/static/sass/style.css HTTP/1.1" 304 0
[16/Mar/2013 15:16:52] "GET /site_media/static/foundicons/foundation_icons_general/stylesheets/general_foundicons.css HTTP/1.1" 304 0
[16/Mar/2013 15:16:52] "GET /site_media/static/foundation/js/vendor/custom.modernizr.js HTTP/1.1" 304 0
[16/Mar/2013 15:16:52] "GET /site_media/static/foundation/js/vendor/zepto.js HTTP/1.1" 304 0
[16/Mar/2013 15:16:52] "GET /site_media/static/foundation/js/foundation.min.js HTTP/1.1" 304 0
[16/Mar/2013 15:16:52] "GET /site_media/static/img/logo.png HTTP/1.1" 304 0
[16/Mar/2013 15:16:52] "GET /site_media/static/img/navy_blue_min.png HTTP/1.1" 304 0
[16/Mar/2013 15:16:52] "GET /site_media/static/img/sub.png HTTP/1.1" 304 0
[16/Mar/2013 15:16:52] "GET /site_media/static/foundicons/foundation_icons_general/fonts/general_foundicons.woff HTTP/1.1" 304 0
[16/Mar/2013 15:16:52] "GET / HTTP/1.1" 200 14
index
ajax