Django flatpages的观点?

时间:2011-11-29 21:14:56

标签: django django-templates django-views django-flatpages

我使用以下内容来确定与索引页面匹配的模板上的模板,在该模板中,我根据AJAX请求确定要加载的模板:

def home(request):

    if request.is_ajax():
        template = "ajax.html"
    else:
        template = "index.html"

    entries = posts.objects.all()[:10]
    return render_to_response(template, {'posts' : entries}, context_instance=RequestContext(request))

但是,我没有关于我的views.py中提到的flatpages的任何内容,我怎么能做类似的request.is_ajax()检查flatpages的模板?

谢谢!

1 个答案:

答案 0 :(得分:5)

知道了!

urls.py

url(r'^(?P<url>.*)$', 'Alpha.blog.views.flatpage', {}, 'flatpage'),

views.py:

def flatpage(request, url):
    try:
        if url == '':
            url = 'home/'
        flatpage = FlatPage.objects.get(url="/%s" % url)
    except:
        pass

    if request.is_ajax():
        template = 'pages/ajax.html'
    else:
        template = 'pages/default.html'

    context_instance=RequestContext(request)
    context_instance.autoescape=False
    return render_to_response(template, {'flatpage': flatpage}, context_instance)