NoReverseMatch“无效的新功能或模式名称”

时间:2018-08-25 17:39:34

标签: python django url

enter image description here

已经几个小时了,我似乎找不到我的错误在哪里。我的想法是在我的category.html文件或new_topic.html文件中。我正在尝试将新主题添加到类别。有多个类别,输入的主题将根据用户的选择转到特定的类别。每次我单击链接将新主题添加到某个类别时,都会收到上面显示的错误。其他一切都很好。

urls.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'),
]

view.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)

category.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 %}

2 个答案:

答案 0 :(得分:0)

问题似乎出在

new_topic.html 模板中
{% url 'blogging_logs:category' category.id %}

您正在寻找blogging_logs:category视图,该视图不存在。也许你是说:

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

答案 1 :(得分:0)

您的urls.py中没有名称类别