Home > Software engineering >  Reverse for 'topic' with arguments '('',)' not found. 1 pattern(s) tri
Reverse for 'topic' with arguments '('',)' not found. 1 pattern(s) tri

Time:07-13

I am getting this error:

Reverse for 'topic' with arguments '('',)' not found. 1 pattern(s) tried: ['topics/(?P<topic_id>[0-9] )/\\Z']

which I believe is stemming from my urls.py:

from django.urls import path
from . import views

urlpatterns=[
    #your paths go here
    path('', views.index, name='index'), # home page
    path('topics/', views.topics, name='topics'), 
    path('topics/<int:topic_id>/', views.topic, name='topic') # add id arguemnt since each entry is linked to topic by id   
]

here is the topics.html which links to topic.html:

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

{% block content %} 

    <p>Topics</p>

    <ul>
        {% for topic in topics %}  <!--passed through context i guess?-->
            <li>
                <a href = "{% url 'topic' topics.id %}">{{ topic }}</a>
            </li>
        {% empty %} 
            <li>No topics have been added yet.</li>
        {% endfor %}
    </ul>

{% endblock content %}

and here is the views.py code for topic:

def topic(request, topic_id):
    topic = Topic.objects.get(id=topic_id)
    entries = topic.entry_set.order_by('-date_added') # minus sign indicates reverse order
    context = {'topic':topic, 'entries':entries}
    return render(request, 'learning_logs/topic.html', context)

Thanks for any help!

CodePudding user response:

in your html not topics.id you need topic.id

   <a href = "{% url 'topic' topic.id %}">{{ topic }}</a>

or maybe

   <a href = "{% url 'topic' topic_id=topic.id %}">{{ topic }}</a>
  • Related