On Mon, Jan 8, 2018 at 11:50 AM, Ronnie Raney <[email protected]> wrote:

> Also, would something like this work, and would it be a way to create the
> view?
>

No, it won't work, you should read
https://docs.djangoproject.com/en/2.0/topics/class-based-views/intro/

>
> 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(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)
>
>
CBV have a standarized way of getting their object, "get_object()", you
have to override that method (or override the "get_queryset()") and make it
return your random post (which you already know). But also, you may need to
hack more the DetailView because it expects an id - and in the case of
random you won't have it, you are choosing the id at random.

Using a "lower level" CBV, like TemplateView may be easier, but if you
can't read
https://docs.djangoproject.com/en/2.0/topics/class-based-views/intro/
because of the hurry, a FBV will work and it's not a bad option :)


>
>
> --
> 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 [email protected].
> To post to this group, send email to [email protected].
> 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
> <https://groups.google.com/d/msgid/django-users/e647327a-6f64-4cb1-a4fc-c5188db010ee%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
> 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 [email protected].
To post to this group, send email to [email protected].
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/CA%2BFDnhLEpDjCqRR91Arw%3DFSzHFZBFq-DecPQCxU3_hwPGbOL8Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to