Django 平面页面的视图?

发布于 2024-12-18 18:59:04 字数 435 浏览 0 评论 0原文

我使用以下内容来确定与索引页面匹配的模式上的模板,其中我根据 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))

但是,我没有任何有关 view.py 中提到的平面页面的信息,我怎样才能对平面页面的模板进行类似的 request.is_ajax() 检查?

谢谢!

I use the following to determine the template on a pattern that matches the index page, in which I determine which template to load based on if it's an AJAX request:

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))

However, I don't have anything regarding flatpages mentioned in my views.py, how can I do a similar request.is_ajax() check for the templates of flatpages?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌面超妹 2024-12-25 18:59:04

知道了!

urls.py

url(r'^(?P<url>.*)

视图.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)
, 'Alpha.blog.views.flatpage', {}, 'flatpage'),

视图.py:

Got it!

urls.py

url(r'^(?P<url>.*)

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)
, 'Alpha.blog.views.flatpage', {}, 'flatpage'),

views.py:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文