0

my blog has 5 kinds of post,3 have detail page and 2 with no detail page,I have to search previous and next post by post id ,if the previous doesn't have detail page,continue , here is my code:

class Post_detail_View(View):
def get(self,request,post_id):
    try:
        post = Post.objects.select_related('standardpost', 'quotepost', 'linkpost', 'audiopost', 'videopost').get(pk=post_id)
        previous = self.get_previous(post_id)
        next_post = self.get_next(post_id)

        if post.type == 'standard':
            content = markdown.markdown(post.standardpost.content, extensions=['markdown.extensions.extra',
                                                                                'markdown.extensions.codehilite',
                                                                                'markdown.extensions.toc', ])
            return render_to_response('post-detail.html', locals())
        elif post.type == 'audio':
            content = markdown.markdown(post.audiopost.content, extensions=['markdown.extensions.extra',
                                                                            'markdown.extensions.codehilite',
                                                                            'markdown.extensions.toc', ])
            return render_to_response('post-detail.html', locals())

        elif post.type == 'video':
            content = markdown.markdown(post.videopost.content, extensions=['markdown.extensions.extra',
                                                                            'markdown.extensions.codehilite',
                                                                            'markdown.extensions.toc', ])
            return render_to_response('post-detail.html', locals())
    except:
        raise Http404

def get_previous(self,post_id):

    post_id = int(post_id)
    if post_id > 1:
        try:
            previous = Post.objects.select_related('standardpost', 'quotepost', 'linkpost', 'audiopost','videopost').get(pk=str(post_id - 1))
            if previous.type == 'quote' or previous.type == 'link':
                if post_id - 1 > 1:
                    self.get_previous(str(post_id - 1))
                else:
                    return 0
            else:
                return previous
        except:
            return 0
    else:
        return 0

def get_next(self,post_id):
    post_id = int(post_id)
    try:
        next_post = Post.objects.select_related('standardpost', 'quotepost', 'linkpost', 'audiopost','videopost').get(pk=str(post_id + 1))
        if next_post.type == 'quote' or next_post.type == 'link':
            self.get_next(str(post_id + 1))
        else:
            return next_post
    except:
        return 0

when I debug this previous = self.get_previous(post_id) next_post = self.get_next(post_id)

after one or more recursion,before return,they both have objects to return,but they will become None after return,if directly return with no recursion ,it will be ok.

before return: before return

after return: after return

Kayle Best
  • 73
  • 1
  • 5
  • 1
    You've made your code much harder to debug by consistently catching and hiding all exceptions. **Don't do that**. – Daniel Roseman May 30 '17 at 10:18
  • 1
    I've marked this as a duplicate because the immediate issue is the failure to return the result of the recursive call, but note your code has many many more problems than that. In particular, you assume that post IDs will be strictly increasing with no gaps, which is not at all a reasonable assumption. – Daniel Roseman May 30 '17 at 10:22
  • You haven't assign the returned the value edit this line `self.get_previous(str(post_id - 1))` to `previous = self.get_previous(str(post_id - 1))` – Rajan Chauhan May 30 '17 at 10:30
  • @DanielRoseman thanks,I add two return and it works. – Kayle Best May 30 '17 at 11:31
  • @RajanChahan thanks,I add two return and it works. – Kayle Best May 30 '17 at 11:31

0 Answers0