首页 > 解决方案 > 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 %}

标签: pythondjangourl

解决方案


问题似乎出在new_topic.html模板中

{% url 'blogging_logs:category' category.id %}

您正在寻找blogging_logs:category不存在的视图。也许你的意思是:

{% url 'blogging_logs:topics' category.id %}

推荐阅读