tamara
tamara

Reputation: 120

SyntaxError invalid syntax (views.py, line 52)

Do you know why does it say invalid syntax in the return line? Everything seems to be ok I checked. I have replaced tabs with spaces if indentation is a problem.

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

Thanks in advance.

Upvotes: 1

Views: 1706

Answers (3)

Steve Jalim
Steve Jalim

Reputation: 12195

Erm, you're opening three trys and only have two excepts... you need to catch that first try before the return

Upvotes: 3

Zain Khan
Zain Khan

Reputation: 3793

Add a RequestContext in your return statement

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

Upvotes: 0

Zain Khan
Zain Khan

Reputation: 3793

list index seems to be on the line

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

If you know that your query would return a single result then don't use FILTER, replace it with GET and along with it use try except.

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

else you could simple do

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

Upvotes: 0

Related Questions