没有找到反向,Django,第一次

时间:2016-02-01 16:09:04

标签: python django

search.html是显示搜索结果列表的页面,但是当我点击"搜索"按钮它显示一个错误页面"反向找不到" ..我在这里看到相同的问题,但代码不同,所以我没有找到问题的解决方案。

def search(request):
if 'q' in request.GET and request.GET['q']:
    q = request.GET['q']
    words = word.objects.filter(title__icontains = q)
    return render_to_response('dictionary/search.html',
        {'words': words, 'query': q })
else:
    return HttpResponse('Please submit a search term.')


def worder(request, word_id):
    showword = get_object_or_404(word, id = word_id)
    return render(request, 'dictionary/worder.html', {'showword': showword})

网址:

urlpatterns = [
url(r'^$', Dictionary.views.home, name='home'),
url(r'^about/$', Dictionary.views.about, name='about'),
url(r'^search/$', Dictionary.views.search, name='search'),
url(r'^worder/(?P<word_id>[0-9]+)/$', Dictionary.views.worder, name='worder')]

{% url 'worder' word_id %}存在问题:

<li class="listofwords"><a class="foundword" href="{% url 'worder' word_id %}">{{ word.title }}</a></li>

1 个答案:

答案 0 :(得分:3)

您使用的是不存在的变量word_id。您应该使用word.id代替对象词的ID:

<li class="listofwords"><a class="foundword" href="{% url 'worder' word.id %}">{{ word.title }}</a></li>