Skip to main content
 首页 » 编程设计

python之Django:NoReverseMatch 不是有效函数

2024年11月24日27unruledboy

在虚拟环境中运行我的本地 Django 开发服务器 (2.2),我遇到了回溯。作为此错误一部分的基本关键字包括“django.urls.exceptions.NoReverseMatch”和“不是有效的 View 函数或模式名称”。

我正在学习尼克·沃尔特 (Nick Walter) 的代码沿用 Udemy 类(class)。部分类(class) Material 涉及使用 Django 编写基本博客。 Nick 的博客模块即将结束。

我想我在某处不准确地引用了一个函数,或者我错误地配置了我的 urlpattern。还有一些其他 SO 成员遇到过类似的决议错误,通常涉及更正拼写错误。我已经尝试删除我的 post_details View 函数的复数形式。我已经尝试使用不同的正则表达式组合(和不使用)来改变我的 urls.py。我觉得我在这里忽略了一些微不足道的事情。

我已经到了将我的代码与类(class)讲师的模块末尾源代码进行比较的地步,但我终究无法弄清楚我做错了什么。

这是我的代码:

urls.py:

from django.urls import path, re_path 
# from . import views 
from posts.views import * 
from redactors.views import * 
from counters.views import * 
from django.conf.urls.static import static 
from django.conf import settings 
 
urlpatterns = [ 
  path('admin/', admin.site.urls), 
  path('', home, name='home'), 
  path('result/', result, name='result'), 
  path('seth/', counters, name='seth'), 
  path('james/', posts, name='james'), 
  re_path(r'^posts/(?P<post_id>[0-9]+)/$', post_details, name='james'), 
  path('simon/', redactors, name='simon'), 
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)  

posts/views.py:

from django.shortcuts import redirect, render, get_object_or_404 
from .models import Post 
 
def posts(request): 
    posts = Post.objects.order_by('-pub_date') 
    return render(request, 'posts/james.html', {'posts':posts}) 
 
def post_details(request, post_id): 
    post = get_object_or_404(Post,pk=post_id) 
    return render(request, 'posts/detailed.html', {'post':post}) 

posts/templates/posts/detailed.html:

<!DOCTYPE html> 
<html lang="en"> 
  <head> 
    <meta charset="utf-8"> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
     
    <title>Neptune Blog</title> 
     
  </head> 
  <body> 
   
  <h1>{{ post.title }}</h1> 
 
      <a href="{% url 'post_details' post.id %}"><h1>{{ post.title }}</h1></a> 
      <h4><span class="glyphicon glyphicon-time" aria-hidden="true"></span> {{ post.pub_date_pretty }}</h4> 
      <br /> 
      <img src="{{ post.image.url }}" class="img-responsive center-block" style="max-height:300px;" /> 
      <br /> 
      <p>{{ post.body }}</p> 
      <br /> 
      <br /> 
</body> 
</html> 

我希望我的博客能够正确加载和呈现。

Here is a screenshot of the Django debugger showing the traceback in my web browser.

这是完整的 shell 回溯:

File "/home//dev/projects/python/2018-and-2019/CC_Redact_Iter2/venv/lib/python3.7/site-packages/django/urls/resolvers.py", line 660, in _reverse_with_prefix

raise NoReverseMatch(msg)

django.urls.exceptions.NoReverseMatch: Reverse for 'post_details' not found. 'post_details' is not a valid view function or pattern name.

请您参考如下方法:

如果我们看一下 urls.py ,我们看到:

# urls.py 
 
urlpatterns = [ 
    # ... 
    re_path(r'^posts/(?P<post_id>[0-9]+)/$', post_details, <b>name='james'</b>), 
    # ... 
]

所以 View 的名称是james , 不是 post_details .因此有两种选择:

  1. 使用james当您引用 View 时;或
  2. 将 View 名称更改为 post_details

使用james作为 View 的名称

因此您应该将 url 写为:

<a href="{% url <b>'james'</b> post_id=post.id %}"><h1>{{ post.title }}</h1></a>

更改 View 的名称

或者您当然可以更改 View 的名称(将 name='james' 更改为 name='post_details' ,因为这可能是一个更好的名称)。在这种情况下,您将需要更改已经引用 james 的所有内容到新 View :

# urls.py 
 
urlpatterns = [ 
    # ... 
    re_path(r'^posts/(?P<post_id>[0-9]+)/$', post_details, <b>name='post_details'</b>), 
    # ... 
]

在这里更改 View 的名称甚至更重要,因为现在有两个 名称为'james' 的 View ,这会导致很多困惑。