2016-12-15 52 views
0

Python/Django初學者在這裏。我遇到了這個錯誤:URLconf與URL模式不匹配用於加載模板

Using the URLconf defined in learning_log.urls, Django tried these URL patterns, in this order:

  1. ^admin/

  2. ^$ [name='index']

  3. ^topics/$ [name='topics']

  4. ^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速成班:一個動手,基於項目的我的教程編程簡介。

任何幫助將不勝感激。

+0

可能的複製[什麼是NoReverseMatch錯誤,以及如何解決它?](http://stackoverflow.com/questions/38390177/what-is-a-noreversematch-error-and-how-do-i-fix-it) – e4c5

+1

您需要以顯示呈現您單擊的URL來獲取該錯誤的模板。顯然,如錯誤消息所示,「{%url%}」標籤根本沒有被解析。 –

回答

-2

你有你的名字空間定義爲topic,所以不是

{% url 'learning_logs:topic' topic.id %} 

使用

{% url 'topic' topic.id %} 
0

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') 
] 

最後瀏覽的網址,詳細列出它調用一個話題,當Django發現一個匹配該模式的URL時,你會告訴它它調用了視圖函數主題()再次?我覺得應該是主題(), 所以應該

**# Detail page for a single topic 
    url(r'^topics/(?P<topic_id>\d+)/$', views.topic, name='topic')** 
-2

它會是這樣

url(r'^topics/(?P<topic_id>\d+)/$', views.topics, name='topic') 

沒有與此表達錯誤:的unbalanced parenthesis.

+0

(?P \ d +) – ak2ree

+1

這不提供問題的答案。你可以[搜索類似的問題](// stackoverflow.com/search),或者參考頁面右側的相關和鏈接問題來找到答案。如果你有一個相關但不同的問題,請[提出一個新問題](// stackoverflow.com/questions/ask),幷包含一個鏈接來幫助提供上下文。請參閱:[提問,獲得答案,沒有分心](// stackoverflow.com/tour) –