Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4338634
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:06:30+00:00 2026-05-21T11:06:30+00:00

I am creating little blog app. I want to return post, and (my problem),

  • 0

I am creating little blog app. I want to return post, and (my problem), previous and next posts, so I can display them together with post. My current decision is very bad, because not only I load all Post objects, but also convert QuerySet to list. Here is a snippet from views.py:

def post(request, url):
        try:
            post = Post.objects.get(slug = url)
        except Post.DoesNotExist:
            raise Http404()
        posts = Post.objects.all().order_by('-date')
        index = list(posts).index(post)
        if index > 0:
            ppost = posts[index - 1] #previuos post
        else:
            ppost = None
        if index < len(posts):
            npost = posts[index + 1] #next post
        else:
            npost = None
        if not post.visible:
            raise Http404()
        return render_to_response('post.html',
                                  {'post': post, 'ppost': ppost, 'npost': npost},
                                  context_instance=RequestContext(request))

And here is models.py:

class Post(models.Model):
    slug = models.SlugField(unique=True)
    subject = models.CharField(max_length=150)
    abstract = models.CharField(max_length=300)
    content = models.TextField()
    author = models.ForeignKey(User)
    date = models.DateTimeField(auto_now_add=True)
    visible = models.BooleanField(default=False)
    tags = models.ManyToManyField(Tag, blank = True, null = True)

    def save(self, *args, **kwargs):
        self.abstract = self.content[:297] + "..."
        self.slug = slugify(self.subject)
        super(Post, self).save(*args, **kwargs)

    def __unicode__(self):
        return self.subject

    class Meta:
        ordering = ['-date']

I would appreciate any architectural ideas, how to implement this case. Any ideas actually :). Thank you in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T11:06:31+00:00Added an answer on May 21, 2026 at 11:06 am

    You don’t have to implement fetching the next and previous instances yourself, it’s already builtin: http://docs.djangoproject.com/en/1.3/ref/models/instances/#django.db.models.Model.get_previous_by_FOO

    If you want to tweak this a bit, for example to get the neighboring instances for non-date fields, see this thread on django users

    With these model methods you can create a pager in your template like this:

    <div id="pager">
        {% if object.get_prev_by_title  %}
            <a class="prev" href="{{ object.get_prev_by_title.get_absolute_url }}">
    {{ object.get_prev_by_title }}</a>
        {% endif %}
        {% if object.get_next_by_title  %}
            <a class="next" href="{{ object.get_next_by_title.get_absolute_url }}">
    {{ object.get_next_by_title }}</a>
        {% endif %}
    </div>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a little paint app, and I want to give my painters the
I'm creating a little web app where people can choose colors for different parts
I am creating little blog app. Here is a snippet from my views: views.py
I've been wondering how to do this for ages. I'm creating a little app,
I want to format the posts on a blog to have stacking 'title divs'
I'm creating a little Silverlight LOB app. I created the app + web in
I'm creating a little app that configures a connected device and then saves the
I have a fairly standard blog app with the usual post and comments controller/models
I am creating a little app that will let a user read what's in
I've been charged with creating a little GUI launcher app to go on a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.