【发布时间】:2017-10-10 10:12:32
【问题描述】:
第二次发布关于 NoReverseMatch 的信息。我似乎无法很好地理解错误,无法在我的代码中找到错误。上次是因为我没有给我的 url 正则表达式中的字段命名,但现在这不是问题所在。我知道有一百万件事可能会出错,但由于某种原因我似乎找不到它。
错误是:
NoReverseMatch 在 /david/Physics/a/ 使用关键字参数 '{'category_name': 'Physics', 'information_name': 'Test info', 'subcategory_name': '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 时如何识别实际错误,我将不胜感激
非常感谢!
【问题讨论】:
-
请您将答案标记为正确。谢谢
标签: python django django-templates