Reputation: 31
Python/Django beginner here - I get this error:
Reverse for 'topic' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['topics/(?P\d+)/$']
when trying to load my template. This is my template:
{% 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 for now</li>
{% endfor %}
</ul>
{% endblock content %}
This is my views.py
from django.shortcuts import render
from .models import Topic
# Create your views here.
def index(request):
'''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)
I have been on this a while now, read some previous answers here, but they all had to do with auth/login not working. Also tried removing the '' after the url as some answers suggested but it didnt work. I am using Python Crash Course: A Hands-On, Project-Based Introduction to Programming for my tutorials.
Any help will be appreciated.
Finally, this is my urls.py code from django.conf.urls import url from . import views
urlpatterns = [
# Home page
url(r'^$', views.index, name='index'),
url(r'^topics/$', views.topics, name='topics'),
url(r'^topics/(?P<topic_id>\d+)/$', views.topics, name='topic'),
Upvotes: 1
Views: 627
Reputation: 474221
According to the error, there was an argument passed into the url
tag, but it was empty:
Reverse for 'topic' with arguments '('',)'...
That's because of the topic_id
variable, it is not defined. You should use topic.id
instead:
<a href="{% url 'learning_logs:topic' topic.id %}">{{ topic }}</a>
Upvotes: 3