0

你知道为什么它在返回行中显示无效语法吗?一切似乎都很好,我检查了。如果缩进有问题,我已经用空格替换了制表符。

def detail(request, sl):
    try:
        post = Post.objects.filter(slug=sl)[0]
        try:
            previous_post = post.get_previous_by_published()
        except:
            previous_post = ""
        try:
            next_post = post.get.next_by_published()
        except:
            next_post = ""
    return render_to_response('blog/detail.html',{'post':post,
                                                  'next_post':next_post,
                                                  'previous_post':previous_post,
                                                 },)

提前致谢。

4

3 回答 3

2

呃,你开了三个trys 并且只有两个excepts ......你需要先抓住它,try然后再return

于 2012-06-05T12:49:27.873 回答
0

列表索引似乎在线

post = Post.objects.filter(slug=sl)[0]

如果您知道您的查询将返回单个结果,则不要使用 FILTER,将其替换为 GET 并与它一起使用 try except。

try:
    post = Post.objects.get(slug = sl)
except:
    pass #something

否则你可以简单地做

try:
    post = Post.objects.filter(slug = sl)[0]
except IndexError, e:
    pass #something
于 2012-06-05T12:22:03.540 回答
0

在您的退货声明中添加 RequestContext

from django.template.context import RequestContext

return render_to_response('blog/detail.html',{'post':post,
                                              'next_post':next_post,
                                              'previous_post':previous_post,
                                             },
                          context_instance=RequestContext(request))
于 2012-06-05T12:33:22.287 回答