【问题标题】:Django NoReverseMatch URL patternDjango NoReverseMatch URL 模式
【发布时间】:2023-03-10 22:01:01
【问题描述】:

我被这个错误困住了,我无法解决它。这是一个 NoReverse 匹配错误。我最初的想法是我的 url 模式是这样写的。我尝试了许多不同的 url 模式,但它似乎不起作用。我不熟悉何时使用 path 与 re_path。

错误:

django.urls.exceptions.NoReverseMatch: Reverse for 'topic' with arguments '('',)' not found. 1 pattern(s) tried: ['topic/(?P<entry_id>\\d+)/$']

文件 urls.py:

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

views.py:

from django.shortcuts import render

from .models import Category, Entry, Topic
# Create your views here.


def index(request):
    """The home page for Learning Logs"""
    return render(request, 'blogging_logs/index.html')


def categories(request):
    """show all categories"""
    categories = Category.objects.all()
    context = {'categories': categories}
    return render(request, 'blogging_logs/categories.html', context)


def topics(request, category_id):
    """Show all topics for a single category"""
    category = Category.objects.get(id=category_id)  # get category that was requested
    topics = category.topic_set.all()  # get all topics associated with category that was requested
    context = {'category': category, 'topics': topics}
    return render(request, 'blogging_logs/category.html', context)


def topic(request, entry_id):
    """Show entry for single topic"""
    topic = Topic.objects.get(id=entry_id)
    entries = topic.entry_set.all()
    context = {'topic': topic, 'entries': entries}
    return render(request, 'blogging_logs/topic.html', context)

Categories.html

{% block content %}
  <p>Categories</p>
  <ul>
    {% for category in categories %}
    <li><a href="{% url 'blogging_logs:topics' category.id %}">{{ category }}</a></li>
    {% empty %}
      <li>No categories entered yet.</li>
      {% endfor %}
  </ul>
{% endblock content %}

Category.html

{% block content %}
  <h1>{{ Categories }}</h1>

  <p>Topics:</p>
  <ul>
      {% for topic in topics %}
        <li><a href="{% url 'blogging_logs:topic' entry.id %}">{{ topic }}</a></li>
        <p>{{topic.date_added|date:'M d, Y H:i' }}</p>
        {% empty %}
          <li>No categories entered yet.</li>
      {% endfor %}
  </ul>
{% endblock content %}

【问题讨论】:

    标签: python django url


    【解决方案1】:

    类别.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>
    {% endblock content %}
    

    它是topic.id 而不是entry.id,Category.html 中没有名为entry 的变量。

    【讨论】:

    • 感谢修复它。严重的是要疯了。就我所知... topic.id 是否与 entry_id 进行比较?
    猜你喜欢
    • 2019-07-26
    • 2018-12-17
    • 2023-03-28
    • 2013-09-26
    • 2013-01-19
    • 2015-07-18
    • 1970-01-01
    • 2020-09-19
    • 2017-03-14
    相关资源
    最近更新 更多