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_detail.html'

# Random view
def random_post(request):
    post_ids = Post.objects.all().values_list('post_id', flat=True)
    random_obj = Post.objects.get(id=random.choice(post_ids))
    context = {'random_obj':random_obj,}
    return render(request, 'blog/random_post.html', context)

*urls.py*

urlpatterns = [
    ....
    path('post/<int:pk>/', views.PostDetailView.as_view(), 
name='post_detail'), ### I included this to show my normal Post Detail view
    path('post/<int:pk>/', views.random_post, name='random_post'),
    ....
]

The error I'm getting now has to do with template rendering. I am confused 
about what I should be using and where - id, post_id, pk, etc.

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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@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.
For more options, visit https://groups.google.com/d/optout.

Reply via email to