使用模板的Django NoReverseMatch(再次)

时间:2017-05-11 12:03:41

标签: python django django-templates

第二次发布关于NoReverseMatch的信息。我似乎无法理解错误,足以在我的代码中找到错误。上次是因为我没有给我的url正则表达式中的字段命名,但现在不是问题。我知道有一百万件事可能会出错,但出于某些原因我似乎无法找到它。

错误是:

   / david / Physics / a /的NoReverseMatch   反转'信息'使用关键字参数' {' category_name':' Physics',' information_name':'测试信息',' subcategory_name& #39;:' a'}'未找到。尝试了1种模式:[' david /(?P [a-zA-Z _] +)/(?P [a-zA-Z _] +)/(?P [a-zA-Z_] +)/ $']

导致错误的模板是:

<h1>The items for {{ category }}/{{ subcategory }}</h1>
{% if items %}
    <ul>
    {% for item in items %}
        <li><a href="{% url 'information' category_name=category subcategory_name=subcategory information_name=item %}">{{ item }}</a></li>
    {% endfor %}
    </ul>
{% else %}
    <h4>No items for that subcategory</h4>
{% endif %}

views.py:

from django.shortcuts import render, get_list_or_404, get_object_or_404
from django.http import HttpResponse 

from .models import Category, Subcategory, Information

# Create your views here.

def index(request):
    return render(request, 'basic_web/index.html')

def search(request):
    return HttpResponse('Here you can search!')

def browse(request):
    categories = Category.objects.all()
    context = {'categories': categories}
    return render(request, 'basic_web/browse.html', context)

def view_category(request, category_name):
    category = get_object_or_404(Category,name__iexact=category_name)
    subcategories = get_list_or_404(Subcategory, parent=category)
    context = {'category': str(category), 'subcategories': map(str, subcategories)}
    return render(request, 'basic_web/category.html', context)

def view_subcategory(request, category_name, subcategory_name):
    category = get_object_or_404(Category,name__iexact=category_name)
    subcategory = get_object_or_404(Subcategory, name__iexact=subcategory_name, parent=category)
    items = get_list_or_404(Information, subcategory=subcategory, category=category)
    context = {'category': str(category), 'subcategory': str(subcategory), 'items': map(str,items)}
    return render(request, 'basic_web/subcategory.html', context)

def view_information(request, category_name, subcategory_name, information_name):
#    category = get_object_or_404(Category,name__iexcat=category_name)
#    subcategory = get_object_or_404(Subcategory, name__iexact=subcategory_name, parent=category)
#    information = get_object_or_404(Information, name__iexact=information_name, parent=subcategory)
    return HttpResponse('{0} {1} {2}'.format(category_name, subcategory_name, information_name))

urls.py:

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^search/$', views.search, name='search'),
    url(r'^browse/$', views.browse, name='browse'),
    url(r'^(?P<category_name>[a-zA-Z_]+)/$', views.view_category, name='category'),
    url(r'^(?P<category_name>[a-zA-Z_]+)/(?P<subcategory_name>[a-zA-Z_]+)/$',
        views.view_subcategory, name='subcategory'),
    url(
    r'^(?P<category_name>[a-zA-Z_]+)/(?P<subcategory_name>[a-zA-Z_]+)/(?P<information_name>[a-zA-Z_]+)/$',
        views.view_information, name='information'),
    ]

如果您能在Django说NoReverseMatch

时能给我一些如何识别实际错误的提示,我也非常感激。

非常感谢!

1 个答案:

答案 0 :(得分:1)

这是您的网址定义。

r'^(?P<category_name>[a-zA-Z_]+)/(?P<subcategory_name>[a-zA-Z_]+)/(?P<information_name>[a-zA-Z_]+)/$',
    views.view_information, name='information'),

据我所知,它不允许网址中的空格。但是你显然在你的一个值中有一个空格

'{'category_name': 'Physics', 'information_name': 'Test info', 'subcategory_name': 'a'}'