Hi.

Since you do have show post url, I would create random view just to
redirect to show post view with random post id.

7.1.2018 5.49 ap. "Ronnie Raney" <[email protected]> kirjoitti:

> 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...
>
> <a class="btn btn-primary btn-lg" href="{% url 'random_post' %}"
>> role="button">Random Blog Post</a>
>
>
> 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 [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/c68e062a-c2a3-434e-aa70-cfcf3e10e600%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/c68e062a-c2a3-434e-aa70-cfcf3e10e600%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/CAHn91oeMjKMpQvwhJVqry_US-XW1OP5q2jgyxYZrAwvQoX6UWA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to