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

> 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 have the same url pointing to different views - 'post/<int:pk>/' - in
what you pasted - that's a problem, in the best case only one will be used,
don't know which. On the other hand, the error is complaining that what it
could resolve has a non-optional id parameter - /post/random/<int:id>/ -
which you are not passing (probably a "{% url 'random_post' %}"). Remove
the <id> from the pattern or add it to the url template tag if you need it.
And you probably need to clean up and fix your urls.py... :)


> --
> 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/176a1d46-d74e-4999-bb86-b90eac05882f%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/176a1d46-d74e-4999-bb86-b90eac05882f%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%2BFDnhJ4L%2BbSRSB-9-1HaPjNciEdkZ-5ETKGEb--npKN_HU4EA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to