date: 2021-11-21title: Djang版本升级后导致的报错 #标题
tags: #标签
categories: python # 分类
Django框架的版本更新迭代速度过快,不同版本之间呢,也会有不同差异,这篇文章主要用来记录下Django 2.0和Django2.0之后的区别。
报错一:Reverse for ‘edit_book’ with arguments ‘(6,)’ not found. 1 pattern(s) tried: [‘editbook/(\d+)/$’]
1、url.py代码内容
from django.urls import path
from app01 import views
urlpatterns = [
path('index/', views.Show_and_add_books.as_view(), name='show_books'),
path('addbook/', views.addbook, name='add_book'),
path(r'editbook/(\d+)/', views.Edit_book.as_view(), name='edit_book'),
path('deletebook/', views.Delete_book.as_view(), name='delete_book'),
]
2、访问web页面报错如下:
3、pycharm控制台报错:
Internal Server Error: /index/
Traceback (most recent call last):
File "D:\software\python\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "D:\software\python\lib\site-packages\django\core\handlers\base.py", line 179, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "D:\software\python\lib\site-packages\django\views\generic\base.py", line 73, in view
return self.dispatch(request, *args, **kwargs)
File "D:\software\python\lib\site-packages\django\views\generic\base.py", line 101, in dispatch
return handler(request, *args, **kwargs)
File "E:\django_project\first_pro\app01\views.py", line 19, in get
return render(request, 'index.html', {'books': all_books})
File "D:\software\python\lib\site-packages\django\shortcuts.py", line 19, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "D:\software\python\lib\site-packages\django\template\loader.py", line 62, in render_to_string
return template.render(context, request)
File "D:\software\python\lib\site-packages\django\template\backends\django.py", line 61, in render
return self.template.render(context)
File "D:\software\python\lib\site-packages\django\template\base.py", line 170, in render
return self._render(context)
File "D:\software\python\lib\site-packages\django\template\base.py", line 162, in _render
return self.nodelist.render(context)
File "D:\software\python\lib\site-packages\django\template\base.py", line 938, in render
bit = node.render_annotated(context)
File "D:\software\python\lib\site-packages\django\template\base.py", line 905, in render_annotated
return self.render(context)
File "D:\software\python\lib\site-packages\django\template\defaulttags.py", line 211, in render
nodelist.append(node.render_annotated(context))
File "D:\software\python\lib\site-packages\django\template\base.py", line 905, in render_annotated
return self.render(context)
File "D:\software\python\lib\site-packages\django\template\defaulttags.py", line 446, in render
url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
File "D:\software\python\lib\site-packages\django\urls\base.py", line 87, in reverse
return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "D:\software\python\lib\site-packages\django\urls\resolvers.py", line 685, in _reverse_with_prefix
raise NoReverseMatch(msg)
django.urls.exceptions.NoReverseMatch: Reverse for 'edit_book' with arguments '(6,)' not found. 1 pattern(s) tried: ['editbook/\\(\\\\d\\+\\)/$']
[21/Nov/2021 22:05:57] "GET /index/ HTTP/1.1" 500 139674
4、解决方法
由于Django升级导致的,在Django 2.0以后的版本,如果在urls.py文件中使用正则匹配,则需要将上面的代码改为如下:
from django.urls import path
from app01 import views
from django.urls import re_path # Django 2.0以后的版本需要通过re_path在路由系统中使用正则匹配
urlpatterns = [
path('index/', views.Show_and_add_books.as_view(), name='show_books'),
path('addbook/', views.addbook, name='add_book'),
# path 要改为 re_path 才可以使用正则
re_path('editbook/(\d+)/', views.Edit_book.as_view(), name='edit_book'),
path('deletebook/', views.Delete_book.as_view(), name='delete_book'),
]