添加 link 返回类别时出现 NoReverseMatch 错误
NoReverseMatch error when adding link to go back to category
我正在用 Django 制作一个论坛网站并设置了类别,每个类别都有不同的主题可以评论。在特定主题页面上,我可以添加 {{ topic }} 但是当我尝试在其上方添加 {{ category }} 时它不会显示。我还试图将 {{ category }} 设为 link,以便他们可以返回到该类别的主页。我已经尝试在 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)
您在该模板的上下文中没有任何名为 category
的内容。
我假设从主题到类别有一个外键:如果是这样,你可以这样做:
<a href="{% url 'speak_space:category' topic.category.id %}">{{ topic.category }}</a>
我正在用 Django 制作一个论坛网站并设置了类别,每个类别都有不同的主题可以评论。在特定主题页面上,我可以添加 {{ topic }} 但是当我尝试在其上方添加 {{ category }} 时它不会显示。我还试图将 {{ category }} 设为 link,以便他们可以返回到该类别的主页。我已经尝试在 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)
您在该模板的上下文中没有任何名为 category
的内容。
我假设从主题到类别有一个外键:如果是这样,你可以这样做:
<a href="{% url 'speak_space:category' topic.category.id %}">{{ topic.category }}</a>