问题
I'm following the official tutorial to learn Django and using 1.5.
I had this link as part of my index template, which was working fine:
<li><a href="/polls/{{ poll.id }}/">{{ poll.question }}</a></li>
however, this is hardcoded and the tutorial suggested a better way was to use:
<li><a href="{% url 'detail' poll.id %}">{{ poll.question }}</a></li>
so that you'll be better of when dealing with huge number of templates and u have to make changes to the url.
Since I made the above change I get the following errors when I run the app:
Exception Type: NoReverseMatch
Exception Value: Reverse for 'detail' with arguments '('',)' and keyword arguments '{}' not found.
My urls.py looks like this:
from django.conf.urls import patterns, url
from polls import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^(?P<poll_id>\d+)/$', views.detail, name='detail'),
url(r'^(?P<poll_id>\d+)/results/$', views.results, name='results'),
url(r'^(?P<poll_id>\d+)/vote/$', views.vote, name='vote'),
)
views.py looks like this:
from django.shortcuts import render, get_object_or_404
from django.http import Http404
from polls.models import Poll
def index(request):
latest_poll_list = Poll.objects.order_by('-pub_date')[:5]
context = {'latest_poll_list': latest_poll_list}
return render(request, 'polls/index.html', context)
def detail(request, poll_id):
poll = get_object_or_404(Poll, pk = poll_id)
return render(request, 'polls/detail.html', {'poll': poll})
my index.html template looks like this:
{% if latest_poll_list %}
<ul>
{% for poll in latest_poll_list %}
<li><a href="{% url 'polls:detail' poll_id %}">{{ poll.question }}</a></li>
{% endfor %}
</ul>
{% else %}
<p> No polls are available.</p>
{% endif %}
Usually I could easily read where the error is coming from and deal with it but in this case I can't spot the cause of the error hence I'm unable to progress with my study. Any help will be greatly appreciated.
回答1:
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
.
回答2:
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. :$
回答3:
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'),
回答4:
I struggled with this for a while. Then I noticed I had put poll.id and not Poll.id with a (capital P)
回答5:
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
回答6:
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.
来源:https://stackoverflow.com/questions/19336076/django-reverse-for-detail-with-arguments-and-keyword-arguments-n