0

Python/Django beginner here. I'm running into this error:

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.

When I am trying to load my topic template. Here is my template:

{% 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 %}

This is my 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)

And this is my urls.py code:

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

I am using Python Crash Course: A Hands-On, Project-Based Introduction to Programming for my tutorials.

Any help would be much appreciated.

  • Possible duplicate of [What is a NoReverseMatch error, and how do I fix it?](http://stackoverflow.com/questions/38390177/what-is-a-noreversematch-error-and-how-do-i-fix-it) – e4c5 Dec 15 '16 at 00:51
  • 1
    You need to show the template that rendered the URL you clicked on to get that error. Clearly, as shown in the error message, the `{% url %}` tag wasn't getting parsed at all. – Daniel Roseman Dec 15 '16 at 10:27

4 Answers4

0

In

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

The last url detailing that it is calling a single topic, you are telling it when Django finds a URL that matches that pattern it calls the view function topics() again? I think it should be topic(), so it should be

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

The current URL, topics/% url 'learning_logs:topic' topic.id %}, didn't match any of these.

In your template for topics you missed "{" before "%".
It should be {% url 'learning_logs:topic' topic.id %}

-2

You have your namespace defined as topic, so instead of

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

use

{% url 'topic' topic.id %}
Andrey Shipilov
  • 1,986
  • 12
  • 14
-2

it will be like this

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

There are error with this expression: unbalanced parenthesis.

Iron Fist
  • 10,739
  • 2
  • 18
  • 34
ak2ree
  • 1
  • 1
    This does not provide an answer to the question. You can [search for similar questions](//stackoverflow.com/search), or refer to the related and linked questions on the right-hand side of the page to find an answer. If you have a related but different question, [ask a new question](//stackoverflow.com/questions/ask), and include a link to this one to help provide context. See: [Ask questions, get answers, no distractions](//stackoverflow.com/tour) – Baum mit Augen Nov 13 '17 at 09:22