Search code examples
pythonhtmldjangourl-routingforum

NoReverseMatch error when adding link to go back to category


I am making a forum site with Django and have set up categories, each with different topics that can be commented on. On the specific topic page, I am able to add {{ topic }} but when I try and add {{ category }} above that it does not show up. I am also trying to make the {{ category }} a link so they can go back to that category's main page. I have tried adding this in the topic.html I keep receiving a NoReverseMatch error.

<p>
    <a href="{% url 'speak_space:category' category.id %}">{{ category }}</a>
</p>

This is the error I receive when that code is included:

NoReverseMatch at /topics/3/
Reverse for 'category' with arguments '('',)' not found. 1 pattern(s) 
tried: ['categories/(?P<category_id>[0-9]+)/$']

Here's my full topic.html page (I added a back button for a temporary fix):

{% extends 'speak_space/base.html' %}
{% block content %}

    <form>
        <input type="button" value="<< go back" onclick="history.back()">
    </form>

    <p>Topic: {{ topic }}</p>

    <p>
        <a href="{% url 'speak_space:new_entry' topic.id %}">Join the convo!</a>
    </p>


    <p>Responses:</p>

    <ul>
    {% for entry in entries %}
        <li>
            <p>{{ entry.date_added|date:'M d, Y H:i' }}</p>
            <p>{{ entry.text|linebreaks }}</p>
            <p>
                <a href="{% url 'speak_space:edit_entry' entry.id %}">Edit</a>
            </p>
        </li>
    {% empty %}
        <li>Nobody has added to this conversation yet. :(</li>
    {% endfor %}
    </ul>

{% endblock content %}

Heres my url.py page:

from django.urls import path

from . import views

app_name = 'speak_space'
urlpatterns = [
    # Home page
    path('', views.index, name='index'),
    # Page that shows all categories(tribes)
    path('categories/', views.categories, name='categories'),
    # Profile page for a category(tribe)
    path('categories/<int:category_id>/', views.category, name='category'),
    # Page that shows all topics.
    path('topics/', views.topics, name='topics'),
    # Detail page for a single topic(conversation)
    # Where you go after you click on someones post
    path('topics/<int:topic_id>/', views.topic, name='topic'),
    # Page for adding a new conversation topic
    path('new_topic/', views.new_topic, name='new_topic'),
    # Page for adding new entry/comment/reply
    path('new_entry/<int:topic_id>/', views.new_entry, name='new_entry'),
    # Page for editing an entry
]

And heres my views.py page

from django.shortcuts import render, redirect

from .models import Category, Topic, Entry
from .forms import TopicForm, EntryForm

def index(request):
    """The home page for speak_space"""
    return render(request, 'speak_space/index.html')

def categories(request):
    """Show all categories."""
    categories = Category.objects.order_by('date_added')
    context = {'categories': categories}
    return render(request, 'speak_space/categories.html', context)

def category(request, category_id):
    """Show a single category(tribe) and all of its topics(convos)."""
    category = Category.objects.get(id=category_id)
    topics = category.topic_set.order_by('-date_added')
    context = {'category': category, 'topics': topics}
    return render(request, 'speak_space/category.html', context)

def topics(request):
    """Show all topics within a category(tribe)."""
    topics = Topic.objects.order_by('date_added')
    context = {'topics': topics}
    return render(request, 'speak_space/topics.html', context)

def topic(request, topic_id):
    """Show a single topic(convo/post) and all of it's replies."""
    topic = Topic.objects.get(id=topic_id)
    entries = topic.entry_set.order_by('-date_added')
    context = {'topic': topic, 'entries': entries}
    return render(request, 'speak_space/topic.html', context)

def new_topic(request):
    """Add a new conversation topic."""
    if request.method != 'POST':
        # No data submitted; create a blank form.
        form = TopicForm()
    else:
        # POST data submitted; process data.
        form = TopicForm(data=request.POST)
        if form.is_valid():
            form.save()
            return redirect('speak_space:topics')

    # Display a blank or invalid form.
    context = {'form': form}
    return render(request, 'speak_space/new_topic.html', context)

def new_entry(request, topic_id):
    """Add a comment/reply to a particular topic/post."""
    topic = Topic.objects.get(id=topic_id)

    if request.method != 'POST':
        # No data submitted; created blank form.
        form = EntryForm()
    else:
        # POST data submitted; process data.
        form = EntryForm(data=request.POST)
        if form.is_valid():
            new_entry = form.save(commit=False)
            new_entry.topic = topic
            new_entry.save()
            return redirect('speak_space:topic', topic_id=topic_id)

    # Display a blank or invalid form.
    context = {'topic': topic, 'form': form}
    return render(request, 'speak_space/new_entry.html', context)

Solution

  • You don't have anything called category in the context for that template.

    I presume that there is a foreign key from topic to category: if so, you can do:

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