Check this out for how to redirect:
https://docs.djangoproject.com/en/2.0/topics/http/shortcuts/#redirect

Check this out for how Django determines the name of primary key fields:
https://docs.djangoproject.com/en/2.0/topics/db/models/#automatic-primary-key-fields

The best advice has already been provided: Get a random post id from your 
model and then redirect to the DetailView of that post.  You can figure it 
out.

On Monday, January 8, 2018 at 10:41:28 AM UTC-6, Ronnie Raney wrote:
>
> Please give me code. Sorry, I can't decipher plain language in forums very 
> well, when talking about code.
>
> My urlpattern:   
>
>  path('post/<int:pk>/', 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 [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/4834d44e-cb98-4d0d-b656-ff2c05a486a8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to