Select random blog post Object from "Post" Model and render In View/Template
Greetings! My first time using this forum. Using Django 2.0 for a project. I'm a beginner so I appreciate your patience. I have a type of blog app, but I don't want the routing/url to be traditional. I want the user to be able to hit a button that says "Random Post" which takes them to a random blog post. I'm not at all sure of the best way to approach this, but I'll share what I have for context. This blog works because I have class-based views using ListView and DetailView. I have a template that shows ALL of the blog posts in chronological order as a ListView, and I have a template that shows each blog post as a DetailView. It works great but I want this "random" functionality for my routing. *Model*: I created a model called Post with all the typical fields, including a unique autofield. My intention was to randomly select a pk using this autofield. I thought about using a property to do some of the querying/logic for my random functionality, but I'm not sure if this is the best way to do it. *View: *I have created a custom method-type view, but it doesn't work. def random_post(request): > > posts = Post.objects.all() > > shuffle (posts) > > random_obj = posts.first() > > context = {'random_obj': random_obj,} > > return render(request, 'blog/random_post.html', context) > > *URL: *I have a 'path' type urlpattern... path('posts/random_post/', views.random_post, name='random_post'), > *TEMPLATE: *Here is the link to my randomly selected blog post... role="button">Random Blog Post The "Detail" template for my blog post has nothing special. The routing seems to work just fine, but the fields are empty. No data is being sent from the model to the view. FYI, this is actually not a blog. I'm using blog logic for the sake of conversation, but I have a very specialized reason for wanting to choose random objects and render them in a view. Thanks in advance! -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c68e062a-c2a3-434e-aa70-cfcf3e10e600%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: Select random blog post Object from "Post" Model and render In View/Template
If I understand you, you suggest using my existing DetailView url which shows the detailed Post, but create a view which selects a random pk. I thought of this but I don’t know how to go about creeating a view that selects a random pk, then applies to the existing urlpattern. My guess is to count() the Post objects, then randomly pick one of the objects. Then somehow grab the primary key, post_id, and somehow insert into a urlpattern. Also point to the correct template. Any help appreciated! -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/493e4623-df7a-4605-8dcc-e88e2db17064%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: Select random blog post Object from "Post" Model and render In View/Template
I am familiar with this from searching all over for a solution. The accepted solution is to create a manager. from django.db.models.aggregates import Count > > from random import randint > > > class PaintingManager(models.Manager): > def random(self): > count = self.aggregate(count=Count('id'))['count'] > random_index = randint(0, count - 1) > return self.all()[random_index] > > If I use this as a template, it might look like this class PostManager(models.Manager): def random(self): count = self.aggregate(count=Count('post_id'))['count'] random_index = randint(0, count - 1) return self.all()[random_index] I tried this earlier in the week and I got nothing but errors, so I was not executing it properly. I think it told me there was no "aggregates". Anyway, moving forward... If this worked to draw a random post_id, then how do I get from this model to my DetailView? Remember, currently I am using a simple class-based view - DetailView - to show blog posts. Would I create another DetailView using PostManager as the model, then create another urlpattern? I really appreciate all of the help with this. It would be nice to get a consensus on how to approach this. To m712: Your solution might be simple, but I'm not seeing it because there is no code. Your suggestion was to create a random_post URL pointing a view that: 1. does shuffle logic 2. returns a redirect to the proper page, ex. /post/$id (is the $ outdated in Django 2.0?) Then you say to make a valid URL using a DetailView. Are you saying to make a different urlpattern than the random_post URL, or are you talking about making the random_post URL valid? Since I'm pretty new to Django, I was confused by your suggestion. -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b6bc9e7a-1edb-4abc-ab8c-bbe280fd2f8d%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: Select random blog post Object from "Post" Model and render In View/Template
def random_post(request): posts_ids = Post.objects.all().values_list("id", flat=True) random_obj = Post.objects.get(id=random.choice(posts_ids)) context = {'random_obj': random_obj,} return render(request, 'blog/random_post.html', context) Is “”id”” actually post_id in my case? This is a view right? not part of the model? -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b1080eca-fd1c-407f-bd26-46f3740a603f%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: Select random blog post Object from "Post" Model and render In View/Template
Thanks Mat-A3K Here's what I have so far... *models.py* class Post(models.Model): post_id = models.AutoField(primary_key=True) all the other fields... *views.py* #Normal (not random) post detail view class PostDetailView(DetailView): model = Post template_name = 'blog/post_detail.html' # Random view def random_post(request): post_ids = Post.objects.all().values_list('post_id', flat=True) random_obj = Post.objects.get(id=random.choice(post_ids)) context = {'random_obj':random_obj,} return render(request, 'blog/random_post.html', context) *urls.py* urlpatterns = [ path('post//', views.PostDetailView.as_view(), name='post_detail'), ### I included this to show my normal Post Detail view path('post//', views.random_post, name='random_post'), ] The error I'm getting now has to do with template rendering. I am confused about what I should be using and where - id, post_id, pk, etc. Reverse for 'random_post' with no arguments not found. 1 pattern(s) tried: ['post\\/random\\/(?P[0-9]+)\\/$'] Please help me figure out my urlpattern, and corrections to my view. I think we are very close! -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/176a1d46-d74e-4999-bb86-b90eac05882f%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: Select random blog post Object from "Post" Model and render In View/Template
Also, would something like this work, and would it be a way to create the view? class RandomDetailView(DetailView): model = Post def random_post(request): post_ids = Post.objects.all().values_list('post_id', flat=True) random_obj = Post.objects.get(id=random.choice(post_ids)) ### Is "id" here supposed to be pk, or post_id? context = {'random_obj':random_obj,} return render(request, 'blog/random_post.html', context) -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/e647327a-6f64-4cb1-a4fc-c5188db010ee%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: Select random blog post Object from "Post" Model and render In View/Template
Ugh. Sorry for the confusion. Here again are the pieces where I need suggestions. Please suggest actual code. I can't decipher things written in plain language on forums. Code is key for me. path('post//', views.PostDetailView.as_view(), name='post_detail'), path('post/*random/???*', views.RandomDetailView.as_view(), name='random_post'),* ### I don't know what to put here* Also my view: def random_post(request): post_ids = Post.objects.all().values_list('post_id', flat=True)* ### Is 'post_id' correct? That's my field name that is my primary key* random_obj = Post.objects.get(id=random.choice(post_ids)) *### What do I put here? id? pk? post_id?* context = {'random_obj':random_obj,} return render(request, 'blog/random_post.html', context) I have tried a bunch of different configurations for my urlpattern. I have tried different variable names in my view. I'm not sure that my data is actually being inserted into my view. I don't know what my urlpattern is supposed to be. I don't know which variables to use in my view, regarding the primary key. -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/d5ba26c4-dacc-4ad4-9050-55c1f44cdd8c%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: Select random blog post Object from "Post" Model and render In View/Template
Please give me code. Sorry, I can't decipher plain language in forums very well, when talking about code. My urlpattern: path('post//', views.PostDetailView.as_view(), name='post_detail'), path('post/*random*/*???*', views.random_post, name='random_post'), *### What do I put here?* My view: def random_post(request): post_ids = Post.objects.all().values_list('*post_id*', flat=True) * ### Is this correct? My field name is post_id - the primary key.* random_obj = Post.objects.get(*pk*=random.choice(post_ids)) *### What do I put here? pk, id, post_id?* context = {'random_obj':random_obj,} return render(request, 'blog/random_post.html', context) > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/764fe76e-9d02-4c08-a4ae-f31346802a81%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: Select random blog post Object from "Post" Model and render In View/Template
Thanks - for clarification. Yes! good advice on remaining calm. Apologies if I seemed intense. If I use this urlpattern, it takes me to the correct template. *path('post/random/', views.random_post, name='random_post'),* But there is no data. I really wanted to include the pk in the URL, to see which one it was picking at random. But it appears the data is not being queried correctly, or it's not being sent over as part of the view. Revised view: *def random_post(request):* *post_ids = Post.objects.all().values_list('post_id', flat=True)* *random_obj = Post.objects.get(post_id=random.choice(list(post_ids))) I am still unsure if this is the correct variable. * *context = {'random_obj':random_obj,}* *return render(request, 'blog/random_post.html', context)* Also, the here are my imports. Maybe there's something missing: *from django.shortcuts import render, redirect* *from .forms import ContactForm* *from django.http import HttpResponse, HttpResponseRedirect* *from django.shortcuts import render, redirect* *from django.core.mail import send_mail, BadHeaderError* *from django.http import HttpResponse, HttpResponseRedirect* *from django.utils import timezone* *from .models import Book, Writer, Translator, Post* *import random* -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/7f118b95-c122-4fc3-bfee-f1d008919b5d%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: Select random blog post Object from "Post" Model and render In View/Template
*You can't change the of a user from a view unless you do a redirect. You can issue a redirect to /post/ where the id is what you picked at random at the "random_post" view* I can't change the *what *of a user from the view? Also, what user? I've tried putting in there and it causes errors. I've also tried , , , , and *Is your template for a "random_obj" or for a "post" object? What does it expect? You can insert a "import ipdb; ipdb.set_trace()" after "context" in your view and inspect if it is right - it will probably be OK.* *Did you rename the post template to random_post and did not change the variables inside? If so, just use something like {"post": random_obj}.* The template is asking for the name of the url - "random_post". The template is loading right now at */post/random/* - confirmed by inspecting the page. The template expects to get the random_post view, I would assume. There are no errors telling me otherwise. Once again, the template is named random_post. So what I think you're suggesting is to change the contex to this: *context = {'random_obj':random_post,}* I also tried switching these: *context = {'random_post':random_obj,}* 'random_obj' is what was established here: *random_obj = Post.objects.get(post_id=random.choice(list(post_ids)))* 'random_post' is the name of my urlpattern, and the name of my template link Am I getting anywhere near understanding what you're saying? All of the things that I've tried are leading me to a template with blank fields. -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/6e2224c1-ebbc-4f44-947b-a9115b56e4e8%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: Select random blog post Object from "Post" Model and render In View/Template
Here's a view that works, in case someone in the future wants cares about this def random_post(request): post_count = Post.objects.all().count() random_val = random.randint(0, post_count-1) post_id = Post.objects.values_list('post_id', flat=True)[random_val] return redirect('post_detail', pk=post_id) -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/41d8a830-c76c-42f8-b475-929603fe052d%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.