2016-12-15 67 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) –