Python 添加链接以返回类别时出现NoReverseMatch错误

Python 添加链接以返回类别时出现NoReverseMatch错误,python,html,django,url-routing,forum,Python,Html,Django,Url Routing,Forum,我正在与Django建立一个论坛网站,并已建立了类别,每个类别都有不同的主题,可以发表评论。在特定主题页面上,我可以添加{{topic}},但当我尝试在上面添加{{category}}时,它不会显示出来。我还试图使{{category}成为一个链接,这样他们就可以返回到该类别的主页。我曾尝试将其添加到topic.html中,但一直收到一个NoReverseMatch错误 <p> <a href="{% url 'speak_space:category' categor

我正在与Django建立一个论坛网站,并已建立了类别,每个类别都有不同的主题,可以发表评论。在特定主题页面上,我可以添加{{topic}},但当我尝试在上面添加{{category}}时,它不会显示出来。我还试图使{{category}成为一个链接,这样他们就可以返回到该类别的主页。我曾尝试将其添加到topic.html中,但一直收到一个NoReverseMatch错误

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

在该模板的上下文中没有任何名为
category
的内容

我假定从主题到类别都有一个外键:如果是这样,您可以执行以下操作:

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


谢谢,它成功了!那个么,为什么这个话题有效呢?模板上下文中的
主题在哪里?这是因为文件名,
topic.html
?。对不起,我是新来的。不是因为你在上下文词典中有主题。
{% 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 %}
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
]
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)
<a href="{% url 'speak_space:category' topic.category.id %}">{{ topic.category }}</a>