错误:
NoReverseMatch at /
Reverse for 'detail' with arguments '(3,)' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'$(?P<college_id>[0-9]+)/$']
错误在这一行:
<li><a href="{% url 'app:detail' college.id %}">{{ college.college_name }}</a></li>
这是整个模板(index.html):
{% if latest_college_list %}
<ul>
{% for college in latest_college_list %}
<li><a href="{% url 'app:detail' college.id %}">{{ college.college_name }}</a></li>
{% endfor %}
</ul>
{% else %}
<p> No colleges available </p>
{% endif %}
观点:
from django.shortcuts import get_object_or_404, render
from .models import College
# Create your views here.
def index(request):
latest_college_list = College.objects.order_by('college_name')
context = {'latest_college_list': latest_college_list}
return render(request, 'app/index.html', context)
def detail(request, college_id):
college = get_object_or_404(College, pk=college_id)
return render(request, 'app/detail.html', {'college':college})
urls.py:
from django.conf.urls import url
from . import views
app_name = "app"
urlpatterns = [
# campusarchitecture.com/
url(r'^$', views.index, name="index"),
# /college_name
url(r'^(?P<college_id>[0-9]+)/$', views.detail, name="detail")
]
root urls conf:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^$', include('app.urls')),
url(r'^login/$', include('login.urls')),
url(r'^admin/', admin.site.urls),
]
任何人都知道问题是什么?
答案 0 :(得分:1)
问题在于:
url(r'^$', include('app.urls')),
应该是
url(r'^', include('app.urls', namespace="app")),
请注意$
应该被删除,因为它表示正则表达式模式的结束,并且它不会发现包含的网址模式。
其次,您需要在namespace
中明确指定include
。有关详情,请参阅documentation here。
同样,在$
网址格式匹配后删除login/
。