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)
path('posts/random_post/', views.random_post, name='random_post'),
<a class="btn btn-primary btn-lg" href="{% url 'random_post' %}" role="button">Random Blog Post</a>
--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django...@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.
--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django...@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.
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]
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]
Is “”id”” actually post_id in my case?
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)
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.
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+unsubscribe@googlegroups.com.
To post to this group, send email to django...@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.
Reverse for 'random_post' with no arguments not found. 1 pattern(s) tried: ['post\\/random\\/(?P<id>[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+unsubscribe@googlegroups.com.
To post to this group, send email to django...@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.
Also, would something like this work, and would it be a way to create the view?
class RandomDetailView(DetailView):model = Postdef random_post(request):post_ids = Post.objects.all().values_list('post_id', flat=True)
random_obj = Post.objects.get(post_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+unsubscribe@googlegroups.com.
To post to this group, send email to django...@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.
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/<int:pk>/', 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
path('post/random/', views.random_post, 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? YES That's my field name that is my primary keyrandom_obj = Post.objects.get(post_id=random.choice(list(post_ids))) ### What do I put here? id? pk? post_id? -> You may have to convert the queryset to a list / evaluate but I'm not sure, you'll have to ty it
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+unsubscribe@googlegroups.com.
To post to this group, send email to django...@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.
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, redirectfrom .forms import ContactFormfrom django.http import HttpResponse, HttpResponseRedirectfrom django.shortcuts import render, redirectfrom django.core.mail import send_mail, BadHeaderErrorfrom django.http import HttpResponse, HttpResponseRedirectfrom django.utils import timezonefrom .models import Book, Writer, Translator, Postimport 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+unsubscribe@googlegroups.com.
To post to this group, send email to django...@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.
--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django...@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.