python - NoReverseMatch "无效的新函数或模式名称"
问题描述
已经几个小时了,我似乎无法找到我的错误在哪里。我的想法是在我的 category.html 文件或 new_topic.html 文件中。我正在尝试将新主题添加到类别中。有多个类别,输入的主题将根据用户选择进入特定类别。每次我单击链接以将新主题添加到某个类别时,我都会收到上面显示的错误。其他一切都很好。
网址.py。文件 :
app_name = 'blogging_logs'
urlpatterns = [
# Home page
path('', views.index, name='index'),
# Show all Categories
path('categories/', views.categories, name='categories'),
# Show all topics associated with category
re_path(r'^topics/(?P<category_id>\d+)/$', views.topics, name='topics'),
# Show single topics
re_path(r'^topic/(?P<entry_id>\d+)/$', views.topic, name='topic'),
# Page for adding a new category
path('new_category/', views.new_category, name='new_category'),
# Page for adding new topics
re_path(r'^new_topic/(?P<category_id>\d+)/$', views.new_topic, name='new_topic'),
]
视图.py 文件:
def new_category(request):
"""Add a new category"""
if request.method != 'POST':
# No data submitted; create a blank formself.
form = CategoryForm()
else:
# POST data submitted; process data
form = CategoryForm(data=request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect(reverse('blogging_logs:categories'))
context = {'form': form}
return render(request, 'blogging_logs/new_category.html', context)
def new_topic(request, category_id):
""" Add new topic to category """
category = Category.objects.get(id=category_id)
if request.method != 'POST':
# No data submitted; create a blank formself.
form = TopicForm()
else:
form = TopicForm(data=request.POST)
if form.is_valid():
new_topic = form.save(commit=False)
new_topic.Category = category
new_topic.save()
return HttpResponseRedirect(reverse('blogging_logs:category', args=[category_id]))
context = {'category': category, 'form': form}
return render(request, 'blogging_logs/new_topic.html', context)
类别.html
{% extends "blogging_logs/base.html" %}
{% block content %}
<h1>{{ Categories }}</h1>
<p>Topics:</p>
<ul>
{% for topic in topics %}
<li><a href="{% url 'blogging_logs:topic' topic.id %}">{{ topic }}</a></li>
<p>{{topic.date_added|date:'M d, Y H:i' }}</p>
{% empty %}
<li>No categories entered yet.</li>
{% endfor %}
</ul>
<a href="{% url 'blogging_logs:new_topic' category.id %}">Add New Topic</a>
{% endblock content %}
new_topic.html
{% extends "blogging_logs/base.html" %}
{% block content %}
<p><a href="{% url 'blogging_logs:category' category.id %}">{{ category }}</a></p>
<form class="" action="{% url 'blogging_logs:new_topic' category.id %}" method="post">
{% csrf_token %}
{{ form.as_p }}
<button name='submit'> Add Topic </button>
</form>
{% endblock content %}
解决方案
问题似乎出在new_topic.html模板中
{% url 'blogging_logs:category' category.id %}
您正在寻找blogging_logs:category
不存在的视图。也许你的意思是:
{% url 'blogging_logs:topics' category.id %}
推荐阅读
- cpu - 实现 9 个 CPU 调度算法的单个程序
- linux - 我可以在 Linux 下使用 Qt Creator 上的 Alt+Tab 禁用自动完成功能吗?
- java - H2 数据库中@GeneratedValue 的问题
- node.js - discord.js EvaluatedPermissions 其他解决方案
- excel - 如果在单元格中选择了值,则为 Msgbox
- php - 没有得到数组 - PHP JSON Explode
- android-source - Aosp 下载问题
- sapui5 - 如何在 SAP Ui5 中添加验证,在输入字段中,用户端必须仅输入具有相应最大长度和最小长度的数字,
- javascript - 如何根据javascript中的对象属性对数据进行分组?
- c# - Xamarin.Forms 重新加载集合视图