django 2.1版本的若干更改语法

版本:Django2.1,python 3.7
一:project的urls引用app的urls方式发生了改变,现在的写法如下:

from django.contrib import admin
from django.urls import path,re_path,include

urlpatterns = [
    path('admin/', admin.site.urls),
    re_path('',include('booktest.urls')),

上述中,booktest是APP名,re_path是引用的方法。
二、查询xxx.has_key:
has_key方法在python2中是可以使用的,在python3中删除了。
比如:
if dict.has_key(word):
改为:

if tuple('word') in dict:

注意:TypeError: unhashable type: ‘list’,所以要将word改为tuple(‘word’)
三、app应用中引入html文件的方法:

from django.conf.urls import re_path
from booktest import views
urlpatterns=[
re_path('', views.cookieTest, name='cookieTest'),
re_path('', views.postTest2, name='postTest2'),
re_path('', views.postTest1, name='postTest1'),
re_path('',views.index,name='index'),
re_path('',views.getTest1,name='getTest1'),
re_path('', views.getTest2, name='getTest2'),
re_path('', views.getTest3, name='getTest3'),
]

其中booktest为项目urls的路径名,其和应用名相同
cookieTest\postTest是html文件名

猜你喜欢

转载自blog.csdn.net/xiaoyaosheng19/article/details/82709298