Я создаю форум на сайте с 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)