Django: Reverse for 'detail' with arguments '('',)' and keyword arguments '{}' not found

江枫思渺然 提交于 2019-11-29 01:30:44

In your index.html you gave poll_id as an argument, but that's just the name the argument will have within the detail function; it is not defined in your template. The actual value you want to call the function with is probably poll.id.

My mistake was a typo on detail.html:

<form action={% url 'polls:vote' polls.id %}" method="post">

should have been

<form action={% url 'polls:vote' poll.id %}" method="post">

It took a while for me to realise the django traceback page was pointing me to the relevant line of code the whole time. :$

This happened to me when I was reading tutorial. I didn't change poll_id to pk:

url(r'^(?P<poll_id>\d+)/$', views.DetailView.as_view(), name='detail'),

vs

url(r'^(?P<pk>\d+)/$', views.DetailView.as_view(), name='detail'),

I struggled with this for a while. Then I noticed I had put poll.id and not Poll.id with a (capital P)

also, in

polls/urls.py

i had spelling error

url(r'^(?P[0-9]+)/$', views.detail, name='details'),

vs the correct code

url(r'^(?P[0-9]+)/$', views.detail, name='detail'),

spent some time looking for the error, so look for proper spelling. lol

The error got sorted out for me after correcting the filter condition in views.py.

snippet of my views.py

def post_share(request, post_id):
        post = get_object_or_404(Post, id=post_id, status='Published')

snippet from my models.py

class Post(models.Model):
STATUS_CHOICES=(
                ('draft','Draft'),
                ('published','Published'),
                )

1st value is stored in the database and the second value is for displaying to the users.

raw data from my mysql DB

+---------------------------------------+-----------+
| title                                 | status    |
+---------------------------------------+-----------+
| Revolution 2020                       | published |
| harry potter and the sorcerer's stone | published |
| harry potter and the cursed child     | draft     |
| five point someone                    | published |
| half girlfriend                       | draft     |
| one night at the call center          | published |
| Django by example                     | published |
+---------------------------------------+-----------+

When I had used "published", I was getting the said error. Once I changed the filter to "Published" it all sorted out.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!