Python/Django初學者在這裏。我遇到了這個錯誤:URLconf與URL模式不匹配用於加載模板
Using the URLconf defined in learning_log.urls, Django tried these URL patterns, in this order:
^admin/
^$ [name='index']
^topics/$ [name='topics']
^topics/(?P\d+)/$ [name='topic']
The current URL, topics/% url 'learning_logs:topic' topic.id %}, didn't match any of these.
當我試圖加載我的主題模板。這裏是我的模板:
{% extends 'learning_logs/base.html' %}
{% block content %}
<p>Topic: {{ topic }}</p>
<p>Entries:</p>
<ul>
{% for entry in entries %}
<li>
<p>{{ entry.date_added|date:'M d, Y H:i' }} </p>
<p>{{ entry.text|linebreaks }}</p>
</li>
{% empty %}
<li>
There are no entries for this topic yet.
</li>
{% endfor %}
</ul>
{% endblock content %}
這是我的views.py:
from django.shortcuts import render
from .models import Topic
def index(request):
'''The home page for Learning Log'''
return render(request, 'learning_logs/index.html')
def topics(request):
'''Show all topics.'''
topics = Topic.objects.order_by('date_added')
context = {'topics': topics}
return render(request, 'learning_logs/topics.html', context)
def topic(request, topic_id):
'''Show a single topic and all its entries.'''
topic = Topic.objects.get(id=topic_id)
entries = topic.entry_set.order_by('-date_added')
context = {'topic': topic, 'entries': entries}
return render(request, 'learning_logs/topic.html', context)
這是我的urls.py代碼:
'''Defines URL patterns for learning_logs.'''
from django.conf.urls import url
from . import views
urlpatterns = [
# Home page
url(r'^$', views.index, name='index'),
# Show all topics.
url(r'^topics/$', views.topics, name='topics'),
# Detail page for a single topic
url(r'^topics/(?P<topic_id>\d+)/$', views.topics, name='topic')
]
我使用Python速成班:一個動手,基於項目的我的教程編程簡介。
任何幫助將不勝感激。
可能的複製[什麼是NoReverseMatch錯誤,以及如何解決它?](http://stackoverflow.com/questions/38390177/what-is-a-noreversematch-error-and-how-do-i-fix-it) – e4c5
您需要以顯示呈現您單擊的URL來獲取該錯誤的模板。顯然,如錯誤消息所示,「{%url%}」標籤根本沒有被解析。 –