【问题标题】:NoReverseMatch at / Reverse for 'movies-detail' with arguments '('Dark',)' not found. 1 pattern(s) tried: ['(?P<name>[^/]+)/(?P<id>[0-9]+)/$']NoReverseMatch at / Reverse for 'movies-detail' with arguments '('Dark',)' 未找到。尝试了 1 种模式:['(?P<name>[^/]+)/(?P<id>[0-9]+)/$']
【发布时间】:2020-08-18 07:56:52
【问题描述】:

我想传递 2 个 url 标签,但得到错误

{% for movie in movies %}
<div class="row">
        <div class="col-md-7">
          <a href="{% url 'movies-detail' movie.name %}">
            <img class="rounded mb-3 mb-md-0"  src="{{ movie.image.url }}" height="200" width="400" alt="">
          </a>
        </div>
        <div class="col-md-5">
            <h2><a class="article-title" href="{% url 'movies-detail' movie.name movie.id %}">{{ movie.name }} {{ movie.id }}</a></h2>
             <h4 class="text-muted">Released Date : {{ movie.date_released|date:"F d, Y " }}</h4>
            <h4 class="article-title" >Producer : {{ movie.producer }}</h4>
            <h4 class="article-title" >Director : {{ movie.director }}</h4>

        </div>
      </div>
            <p> {% ratings movie %}</p>

<hr>
    {% endfor %}

希望在 urls.py 中接收并显示

urlpatterns = [

path('about/', AboutView.as_view(), name='blog-about'),
path('user/<str:username>/', UserPostListView.as_view(), name='user-post'),
path('post/<int:pk>/', PostDetailView.as_view(), name='post-detail'),
path('post/new/<str:name>/', PostCreateView.as_view(), name='post-create'),
path('', MoviesListView.as_view(), name='blog-home'),
path('<str:name>/<int:id>/',MoviesDetailListView.as_view(), name='movies-detail'),
path('post/<int:pk>/update/', PostUpdateView.as_view(), name='post-update'),
path('post/<int:pk>/delete/', PostDeleteView.as_view(), name='post-delete'),

]

【问题讨论】:

  • 你能把所有的网址都贴出来吗?
  • 为什么movie.name后面有这么多空格?从错误中,您的 url 似乎只捕获了 movie.name(缺少秒参数)。请在电影名称后仅添加 1 个空格
  • 只有一个空格会给出同样的错误

标签: python django django-models django-forms django-templates


【解决方案1】:

with arguments '('Dark',)' 告诉您您只提供了名称,而不是 ID。

movie.id 添加到第一个链接。

<a href="{% url 'movies-detail' movie.name movie.id %}">

movies-detail 页面的第二个链接看起来已经正确了。

【讨论】:

  • 如果它不起作用,那么你应该得到一个不同的错误。如果你真的遇到同样的错误,那么你还没有保存你的更改并重新启动服务器,或者你有另一个{% url %}标签有同样的问题。
  • 谢谢你是对的,我在两个地方都有它,这就是它引发错误的原因
【解决方案2】:

您的错误是由于第一个标签&lt;a href="{% url 'movies-detail' movie.name %}"&gt;。您只提供一个位置参数,其中需要两个。

为这个特定的 url 标签添加一个额外的 url,但带有单独的 name

urls.py

urlpatterns = [path('about/', AboutView.as_view(), name='blog-about'),
    path('user/<str:username>/', UserPostListView.as_view(), name='user-post'),
    path('post/<int:pk>/', PostDetailView.as_view(), name='post-detail'),
    path('post/new/<str:name>/', PostCreateView.as_view(), name='post-create'),
    path('', MoviesListView.as_view(), name='blog-home'),
    path('<str:name>/<int:id>/',MoviesDetailListView.as_view(), name='movies-detail'),
    path('<str:name>/',MoviesDetailListView.as_view(), name='movies-detail-name'),
    path('post/<int:pk>/update/', PostUpdateView.as_view(), name='post-update'),
    path('post/<int:pk>/delete/', PostDeleteView.as_view(), name='post-delete'),
]

【讨论】:

  • 已删除。是我的错字。
猜你喜欢
  • 1970-01-01
  • 2022-07-04
  • 1970-01-01
  • 1970-01-01
  • 2019-04-17
  • 2019-11-01
  • 2020-07-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多