【问题标题】:NoReverseMatch error when adding link to go back to category添加链接以返回类别时出现 NoReverseMatch 错误
【发布时间】:2019-10-12 18:30:08
【问题描述】:

我正在使用 Django 创建一个论坛站点,并设置了类别,每个类别都有可以评论的不同主题。在特定主题页面上,我可以添加 {{ topic }} 但是当我尝试在上面添加 {{ category }} 时它不会显示。我还尝试将 {{ category }} 设为链接,以便他们可以返回该类别的主页。我尝试在 topic.html 中添加此内容,但一直收到 NoReverseMatch 错误。

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

这是包含该代码时我收到的错误:

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

这是我的完整 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 %}

这是我的 url.py 页面:

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
]

这是我的views.py页面

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)

【问题讨论】:

    标签: python html django url-routing forum


    【解决方案1】:

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

    我假设从主题到类别有一个外键:如果是这样,你可以这样做:

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

    【讨论】:

    • 谢谢你成功了!那么为什么主题会起作用呢? topic 在模板的上下文中在哪里?这是因为文件名topic.html?。对不起,我是新人。
    • 不,因为你在上下文字典中有主题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-03
    • 2020-01-20
    • 1970-01-01
    相关资源
    最近更新 更多