Django | 3.0.2 |
djangorestframework | 3.11.0 |
在创建新的djaong_rest fram work项目的时候
启动django项目报错如下
D:\work\djc_wcapp\djc_wcapp\app\urls.py changed, reloading. Watching for file changes with StatReloader Performing system checks... System check identified some issues: WARNINGS: ?: (2_0.W001) Your URL pattern '^gamefile$' has a route that contains '(?P<', begins with a '^', or ends with a '$'. This was likely an oversight when migrating to django.urls.path(). ?: (2_0.W001) Your URL pattern '^gamefile/(?P<gm_id>\d+)$' has a route that contains '(?P<', begins with a '^', or ends with a '$'. This was likely an oversight when migrating to django. urls.path().
url.py中的文件如下
"""djc_wcapp URL Configuration The `urlpatterns` list routes URLs to views. For more information please see: https://docs.djangoproject.com/en/3.0/topics/http/urls/ Examples: Function views 1. Add an import: from my_app import views 2. Add a URL to urlpatterns: path('', views.home, name='home') Class-based views 1. Add an import: from other_app.views import Home 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') Including another URLconf 1. Import the include() function: from django.urls import include, path 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin from django.urls import path,re_path from app import views urlpatterns = [ path(r'^gamefile/(?P<gm_id>\d+)$', views.GameFileDBDeatailView.as_view()), # 增删改查 path(r'^gamefile$',views.GameFileDBView.as_view()), #增删改查 ]
访问对应的url
提示如下
Django version 3.0.2, using settings 'djc_wcapp.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CTRL-BREAK. Not Found: /api/v1/gamefile/1 [23/May/2020 13:40:25] "GET /api/v1/gamefile/1 HTTP/1.1" 404 2342 Not Found: /api/v1/gamefile [23/May/2020 13:40:31] "GET /api/v1/gamefile HTTP/1.1" 404 2336
原因:Django 2.0中的新path()
语法不使用正则表达式.你想要的东西:
url.py修改为re_path
from django.contrib import admin from django.urls import path,re_path from app import views urlpatterns = [ re_path(r'^gamefile/(?P<gm_id>\d+)$', views.GameFileDBDeatailView.as_view()), # 增删改查 re_path(r'^gamefile$',views.GameFileDBView.as_view()), #增删改查 ]
Django 2.0中的新path()
语法不使用正则表达式.你想要的东西:
path('<int:album_id>/', views.detail, name='detail'),
如果要使用正则表达式,可以使用re_path()
.
re_path(r'^(?P<album_id>[0-9])/$', views.detail, name='detail'),
旧的url()
仍然有效,现在是re_path的别名,但将来可能会被弃用.
url(r'^(?P<album_id>[0-9])/$', views.detail, name='detail'),