Re: Select random blog post Object from "Post" Model and render In View/Template

2018-01-08 Thread Ronnie Raney
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]

Re: Select random blog post Object from "Post" Model and render In View/Template

2018-01-08 Thread Ronnie Raney
*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'

Re: Select random blog post Object from "Post" Model and render In View/Template

2018-01-08 Thread Ronnie Raney
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

Re: Select random blog post Object from "Post" Model and render In View/Template

2018-01-08 Thread Ronnie Raney
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 vi

Re: Select random blog post Object from "Post" Model and render In View/Template

2018-01-08 Thread Ronnie Raney
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/???*', vi

Re: Select random blog post Object from "Post" Model and render In View/Template

2018-01-08 Thread Ronnie Raney
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_id

Re: Select random blog post Object from "Post" Model and render In View/Template

2018-01-08 Thread Ronnie Raney
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_

Re: Select random blog post Object from "Post" Model and render In View/Template

2018-01-07 Thread Ronnie Raney
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? Th

Re: Select random blog post Object from "Post" Model and render In View/Template

2018-01-07 Thread Ronnie Raney
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

Re: Select random blog post Object from "Post" Model and render In View/Template

2018-01-07 Thread Ronnie Raney
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 cou

Select random blog post Object from "Post" Model and render In View/Template

2018-01-06 Thread Ronnie Raney
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 rando