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 8590029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:11:58+00:00 2026-06-11T23:11:58+00:00

I have created a random blog post link on my site and the code

  • 0

I have created a random blog post link on my site and the code it uses works. When I was creating it there was a problem making it work correctly since it kept saying ‘QuerySet’ object has no attribute ‘slug’, so I came up with a work around. The work around makes more requests to the db than necessary (i think):

from django.http import HttpResponseRedirect
def randompost(request):
    blog_posts = BlogPost.objects.filter(post_status = 'published').order_by('?')[:1]
    blog_title = blog_posts[0]
    blog_post = get_object_or_404(BlogPost, title=blog_title)

    path = '/theblog/post/' + blog_post.slug + '/'

    return HttpResponseRedirect(path)

Ideally I would like a solution like Django redirect URL to latest created blog post

Will it also need the correct http code since it’s a redirect?

The old code with the Queryset error:

blog_post = BlogPost.objects.filter(post_status = 'published').order_by('?')[:1]
path = '/theblog/post/' + blog_post.slug + '/'

return HttpResponseRedirect(path)
  • 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-06-11T23:11:59+00:00Added an answer on June 11, 2026 at 11:11 pm
    1. It’s better to use redirect and reverse URLs;
    2. You could use [] syntax to get first object of QuerySet.

    Updated code:

    from django.shortcuts import redirect
    
    def randompost(request):
        blog_posts = BlogPost.objects.filter(post_status='published').order_by('?')
        latest_post = blog_posts[0]
    
        return redirect('blog_post', latest_post.slug)
    

    Updated:

    URL sample, reverse is done by url name blog_post:

    url(r'^post/(\w+)$', 'view_name', name='blog_post')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Marius Gedminas's excellent blog post , I have created a custom traverser for
i have created random expression and want to link their answers to the keypad
I have created a custom post type - Testimonials - for a WP site
I have created a custom post type named People. I have created a page
I'm trying to create a random quote generator. I have created a function called
I am trying to animate random point that i have created so they move
I have created a lottery script in php. My problem is now selecting more
I have a simple post AR class/table. Its based on the blog tutorial so
I have been writing some software that uses a random string to create a
I have created the following method so as to create unique random numbers .

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.