SyntaxError无效语法(views.py,第52行)

时间:2012-06-05 12:16:08

标签: django list syntax views

你知道为什么它会在返回行中说出无效的语法吗?一切似乎都没关系我查了一下。如果缩进是个问题,我已用空格替换制表符。

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

提前致谢。

3 个答案:

答案 0 :(得分:2)

呃,你打开三个try只有两个except ...你需要在try return >

答案 1 :(得分:0)

列表索引似乎就行了

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

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

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

否则你可以简单地做到

try:
    post = Post.objects.filter(slug = sl)[0]
except IndexError, e:
    pass #something

答案 2 :(得分:0)

在return语句中添加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))