Django中的“没有找到参数的'页面'的反向。尝试了1个模式”

Posted

技术标签:

【中文标题】Django中的“没有找到参数的\'页面\'的反向。尝试了1个模式”【英文标题】:"Reverse for 'page' with no arguments not found. 1 pattern(s) tried" in DjangoDjango中的“没有找到参数的'页面'的反向。尝试了1个模式” 【发布时间】:2022-01-14 17:54:37 【问题描述】:

我最近开始测试和研究 Django,我正在构建一些项目来学习和测试它。 我尝试在文档中寻找答案,但找不到任何答案,如果您可以将文档参考发送给我以使用您的解决方案/提示解决此问题,我将不胜感激。

我试图通过 Django 中的 URL 传递一个参数,当重定向时 url 发生变化并且 int 出现在浏览器上(即 http://127.0.0.1:8000/edit-contact/1 - 1 从“重定向") 但显然视图函数无法识别它。

我用来重定向和发送参数的按钮

<a href="% url 'edit-contact' contato.id%" class="btn btn-primary">
  Edit contact
</a>

urls.py 文件:

urlpatterns = [
    path('', views.index, name='index'),
    path('busca/', views.search, name='busca'),
    path('<int:contato_id>', views.details, name='detalhes'),
    path('edit-contact/<int:contato_id>',
         views.edit_contact, name='edit-contact'),

]

查看我用来捕获请求的函数

@login_required(redirect_field_name='login')
def edit_contact(request, contato_id):
    if request.method != 'POST':
        form = ContatoForm()

        return render(request, 'contatos/detalhes.html', 'form': form, 'contato_id': contato_id)

追溯:

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/edit-contact/1

Django Version: 3.2.9
Python Version: 3.10.0
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'contatos.apps.ContatosConfig',
 'accounts.apps.AccountsConfig']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']


Template error:
In template D:\Gabriel\Codando\studying\django_learning\DjangoProj_agenda\templates\base.html, error at line 0
   Reverse for 'edit-contact' with no arguments not found. 1 pattern(s) tried: ['edit\\-contact/(?P<contato_id>[0-9]+)$']
   1 : % load static%
   2 : 
   3 : <!DOCTYPE html>
   4 : <html lang="pt">
   5 :   <head>
   6 :     % include 'partials/_head.html' %
   7 : 
   8 :     <title>% block 'title' %% endblock %</title>
   9 :   </head>
   10 : 


Traceback (most recent call last):
  File "C:\Users\CARVALHO\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\CARVALHO\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\CARVALHO\AppData\Local\Programs\Python\Python310\lib\site-packages\django\contrib\auth\decorators.py", line 21, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "D:\gabriel\codando\studying\django_learning\DjangoProj_agenda\contatos\views.py", line 76, in edit_contact
    return render(request, 'contatos/detalhes.html', 'form': form, 'contato_id': contato_id)
  File "C:\Users\CARVALHO\AppData\Local\Programs\Python\Python310\lib\site-packages\django\shortcuts.py", line 19, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "C:\Users\CARVALHO\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\loader.py", line 62, in render_to_string
    return template.render(context, request)
  File "C:\Users\CARVALHO\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\backends\django.py", line 61, in render
    return self.template.render(context)
  File "C:\Users\CARVALHO\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\base.py", line 170, in render
    return self._render(context)
  File "C:\Users\CARVALHO\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\base.py", line 162, in _render
    return self.nodelist.render(context)
  File "C:\Users\CARVALHO\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\base.py", line 938, in render
    bit = node.render_annotated(context)
  File "C:\Users\CARVALHO\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\base.py", line 905, in render_annotated
    return self.render(context)
  File "C:\Users\CARVALHO\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\loader_tags.py", line 150, in render
    return compiled_parent._render(context)
  File "C:\Users\CARVALHO\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\base.py", line 162, in _render
    return self.nodelist.render(context)
  File "C:\Users\CARVALHO\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\base.py", line 938, in render
    bit = node.render_annotated(context)
  File "C:\Users\CARVALHO\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\base.py", line 905, in render_annotated
    return self.render(context)
  File "C:\Users\CARVALHO\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\loader_tags.py", line 62, in render
    result = block.nodelist.render(context)
  File "C:\Users\CARVALHO\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\base.py", line 938, in render
    bit = node.render_annotated(context)
  File "C:\Users\CARVALHO\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\base.py", line 905, in render_annotated
    return self.render(context)
  File "C:\Users\CARVALHO\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\defaulttags.py", line 312, in render
    return nodelist.render(context)
  File "C:\Users\CARVALHO\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\base.py", line 938, in render
    bit = node.render_annotated(context)
  File "C:\Users\CARVALHO\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\base.py", line 905, in render_annotated
    return self.render(context)
  File "C:\Users\CARVALHO\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\defaulttags.py", line 446, in render
    url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
  File "C:\Users\CARVALHO\AppData\Local\Programs\Python\Python310\lib\site-packages\django\urls\base.py", line 86, in reverse
    return resolver._reverse_with_prefix(view, prefix, *args, **kwargs)
  File "C:\Users\CARVALHO\AppData\Local\Programs\Python\Python310\lib\site-packages\django\urls\resolvers.py", line 694, in _reverse_with_prefix
    raise NoReverseMatch(msg)

Exception Type: NoReverseMatch at /edit-contact/1
Exception Value: Reverse for 'edit-contact' with no arguments not found. 1 pattern(s) tried: ['edit\\-contact/(?P<contato_id>[0-9]+)$']

【问题讨论】:

如果在contato.id%(在模板中)中的% 之前放置一个空格,是否有效? 这是我第一次尝试,它不起作用 问题出在您的模板中。 contato.id 似乎没有赋值。这就是为什么 django 在没有整数的情况下找不到 edit-contact 的 url 模式的原因。确保在生成编辑联系人 url 时提供一个值。 但是当它重定向时,url 得到整数,如 127.0.0.1:8000/edit-contact/1,我没有把那个 1 放在那里,它是从模板中获取的。这是我的疑问,整数正在被识别,但不是被函数识别 您通过contato_id 将其传递给模板。但在您尝试使用contato.id 访问的模板中。 【参考方案1】:

好吧,我真的不能确定这是一个解决方案还是只是一种解决方法,但我做了一些事情,现在它显然可以工作了。至少我不再收到错误消息了。这就是我所做的。

我为 edit-contact 没有参数时添加了一个 urlpattern,因为这就是错误所指的内容,或者至少是我从中理解的内容。


urlpatterns = [
    path('', views.index, name='index'),
    path('busca/', views.search, name='busca'),
    path('<int:contato_id>', views.details, name='detalhes'),
    path('edit-contact/', views.edit_contact, name='edit-contact'),
    path('edit-contact/<int:contact_id>',
         views.edit_contact, name='edit-contact'),
]

在阅读了 SO 中的很多帖子、阅读了 Django 的文档并独自思考之后,我想出了这个解决方案。如果有人认为我错了或有更好的解决方案,我愿意接受建议,毕竟 我刚刚开始学习 Django

【讨论】:

以上是关于Django中的“没有找到参数的'页面'的反向。尝试了1个模式”的主要内容,如果未能解决你的问题,请参考以下文章

Django - 列中的空值违反了 Django Admin 中的非空约束

Django 框架篇: Django中的Form 组件

django(12)信号

Django中的信号及其用法

Django中的信号及其用法

Django中的信号