2

Going through the Django Tutorial in Python Crash Course and am facing a wall. The error I get is 'Reverse for 'topic' with arguments '('',)' not found. 1 pattern(s) tried: ['topics/(?P<topic_id>\\d+)/$']'.

This is my urls.py

from django.conf.urls import URL


from . import views


urlpatterns = [
# the actual url patter is a call to the url () function, which takes three arguments
# 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.topic, name='topic'),

]

app_name= 'learning_logs'

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) 

topic.html {%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%}

I've read through some of the Django documentation but I'm not comprehending enough to solve this problem on my own. If I need to add some more code to help please let me know. All help is really appreciated.

Edit: Topics.html

{%extends 'learning_logs/base.html'%}

{% block content%}

<p>Topics</p>
<ul>
    {%for topic in topics%}
        <li>
            <a href="{% url 'learning_logs:topic' topic_id%}">{{topic}}</a>
        </li>
    {%empty%}
        <li>No topics have been added yet</li>
    {%endfor%}
</ul>

{% endblock content%}
Amir Afianian
  • 2,679
  • 4
  • 22
  • 46
Kevin H
  • 127
  • 1
  • 7
  • 1
    I'm the author of PCC. You're working from a fairly outdated version of the first edition of the book. Later copies of the first edition used the `path()` structure for urls, as opposed to the older `url()` structure. If you get this issue sorted, you're likely to run into other issues due to how much Django has changed since your copy was printed. [This](https://stackoverflow.com/questions/38390177/what-is-a-noreversematch-error-and-how-do-i-fix-it) is the best SO post I've seen addressing `NoReverseMatch` errors. As for this specific issue, it might be an error in the topics.html file. – japhyr Nov 02 '20 at 01:43
  • `'Reverse for 'topic' with arguments '('',)' not found.` This makes it look like the id for the topic is not being passed through the url. Can you post your topics.html file? – japhyr Nov 02 '20 at 01:45
  • 1
    I know you probably hear it all the time but your book is awesome. Just awesome all around, and thank you for taking the time to help. I have looked over the post and you tagged I think I see where I went wrong. And as requested I have posted the Topics.html. – Kevin H Nov 02 '20 at 12:45

2 Answers2

1

The problem is in topics.html. Here's the loop that shows each topic:

    {%for topic in topics%}
        <li>
            <a href="{% url 'learning_logs:topic' topic_id%}">{{topic}}</a>
        </li>
    {%empty%}
        <li>No topics have been added yet</li>
    {%endfor%}

The variable topic_id is not defined. That should be topic.id, which accesses the id attribute of topic.

japhyr
  • 1,710
  • 2
  • 18
  • 24
0

In your template you are trying to access topic but in views.py you didn't pass topic variable to context. pass topic in your context variable:

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, 'topic': topic}
    return render(request, 'learning_logs/topic.html', context) 
Tasnuva Leeya
  • 2,515
  • 1
  • 13
  • 21