关于Django可以参阅《基于Django的电子商务网站技术》一书。在书中我采用的版本是1.11.4,后来Django升级到了1.11.10,产品仍旧可以正常执行,但是当我把Django升级到2.X版本后,程序就不能运行了,参考了一下网上的资料,发现主要2.0的在路由方面做了改动。在1.X版本中,我们在url.py文章中使用的是from django.conf.urls类url方法。在书中我采用如下代码:
from django.conf.urls import url from django.contrib import admin from goods import views from django.views import static import os BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) urlpatterns = [ url(r'^$', views.index), url(r'^index/$', views.index), url(r'^admin/', admin.site.urls), url(r'^logout/$', views.logout), url(r'^register/$', views.register), url(r'^user_info/$', views.user_info), url(r'^login_action/$', views.login_action), url(r'^search_name/$', views.search_name), url(r'^change_password/$', views.change_password), url(r'^goods_view/$', views.goods_view), url(r'^view_goods/(?P<good_id>[0-9]+)/$', views.view_goods), url(r'^view_chart/$', views.view_chart), url(r'^remove_chart_all/$', views.remove_chart_all), url(r'^remove_chart/(?P<good_id>[0-9]+)/$', views.remove_chart), url(r'^add_chart/(?P<good_id>[0-9]+)/(?P<sign>[0-9]+)/$', views.add_chart), url(r'^update_chart/(?P<good_id>[0-9]+)/$', views.update_chart), url(r'^view_address/$', views.view_address), url(r'^add_address/(?P<sign>[0-9]+)/$', views.add_address), url(r'^delete_address/(?P<address_id>[0-9]+)/(?P<sign>[0-9]+)/$',views.delete_address), url(r'^update_address/(?P<address_id>[0-9]+)/(?P<sign>[0-9]+)/$', views.update_address), url(r'^delete_orders/(?P<orders_id>[0-9]+)/(?P<sign>[0-9]+)/$', views.delete_orders), url(r'^create_order/$', views.create_order), url(r'^view_order/(?P<orders_id>[0-9]+)/$', views.view_order), url(r'^view_all_order/$', views.view_all_order), url(r'^upload/(?P<path>.*)',static.serve,{'document_root':os.path.join(BASE_DIR,'upload')}), url(r'^static/(?P<path>.*)',static.serve,{'document_root':os.path.join(BASE_DIR,'static')}), ]
而在2.X中被from django.urls 中的 path与re_path类取代,关于re_path方法基本上与url方法保持一致,但是不支持static.serve了,而对于path方法不支持正则表达式,主要介绍如下。
应用方法:
from django.urls import path,re_path
使用:
path('view_goods/<int: good_id >/', views.view_goods),
对应于
url(r'^view_goods/(?P<good_id>[0-9]+)/$', views.view_goods),
我们可以看出,这里的:
view_goods:为路径名;
<int: good_id >:int表示数字,参数名为good_id;
views.view_goods仍旧表示为view.py中的view_goods方法中定义的逻辑。
关于int共有以下五类(这五类Django称作转换器):
1.str,匹配除了路径分隔符(/)之外的非空字符串,这是默认的形式
2.int,匹配正整数,包含0。
3.slug,匹配字母、数字以及横杠、下划线组成的字符串。
4.uuid,匹配格式化的uuid,如 075194d3-6885-417e-a8a8-6c931e272f00。
5.path,匹配任何非空字符串,包含了路径分隔符(/)(不能用问号,因为问号在url地址有特殊含义。)
这样我的代码就变成了如下形式。
from django.conf.urls import url from django.urls import re_path from django.contrib import admin from goods import views from django.views import static import os from django.conf.urls.static import static from . import settings urlpatterns = [ re_path(r'^$', views.index), re_path(r'^index/$', views.index), re_path(r'^admin/', admin.site.urls), re_path(r'^logout/$', views.logout), re_path(r'^register/$', views.register), re_path(r'^user_info/$', views.user_info), re_path(r'^login_action/$', views.login_action), re_path(r'^search_name/$', views.search_name), re_path(r'^change_password/$', views.change_password), re_path(r'^goods_view/$', views.goods_view), re_path(r'^view_goods/(?P<good_id>[0-9]+)/$', views.view_goods), re_path(r'^view_chart/$', views.view_chart), re_path(r'^remove_chart_all/$', views.remove_chart_all), re_path(r'^remove_chart/(?P<good_id>[0-9]+)/$', views.remove_chart), re_path(r'^add_chart/(?P<good_id>[0-9]+)/(?P<sign>[0-9]+)/$', views.add_chart), re_path(r'^update_chart/(?P<good_id>[0-9]+)/$', views.update_chart), re_path(r'^view_address/$', views.view_address), re_path(r'^add_address/(?P<sign>[0-9]+)/$', views.add_address), re_path(r'^delete_address/(?P<address_id>[0-9]+)/(?P<sign>[0-9]+)/$',views.delete_address), re_path(r'^update_address/(?P<address_id>[0-9]+)/(?P<sign>[0-9]+)/$', views.update_address), re_path(r'^delete_orders/(?P<orders_id>[0-9]+)/(?P<sign>[0-9]+)/$', views.delete_orders), re_path(r'^create_order/$', views.create_order), re_path(r'^view_order/(?P<orders_id>[0-9]+)/$', views.view_order), re_path(r'^view_all_order/$', views.view_all_order), ]
细心的读者可能会发现,我这里没有定义静态变量,即以前代码中的。
url(r'^upload/(?P<path>.*)',static.serve,{'document_root':os.path.join(BASE_DIR,'upload')}), url(r'^static/(?P<path>.*)',static.serve,{'document_root':os.path.join(BASE_DIR,'static')}),
在Django 2.0中我们可以这样解决。
首先我们打开settings.py文件,找到
# Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/1.11/howto/static-files/
位置,加入如下代码。
STATIC_URL = '/static/' STATIC_URL = '/upload/' STATICFILES_DIRS = ( os.path.join(BASE_DIR,"static"), os.path.join(BASE_DIR,"upload"), )
然后重新打开url.py,在最后下面加上如下代码。
re_path(r'^view_all_order/$', views.view_all_order), ]+ static(settings.STATIC_URL, document_root = settings.STATICFILES_DIRS)+static(settings.STATIC_URL, document_root = settings.STATICFILES_DIRS)
最后一点很重要,我们在启动的时候一定要加上--insecure参数,即
python manage.py runserver 0.0.0.0:8000 --insecure
经过这番改造,在运行我的接口测试代码,全部通过。
版权声明:本文出自51Testing原创,51Testing软件测试网及相关内容提供者拥有内容的全部版权,未经明确的书面许可,任何人或单位不得对本网站内容复制、转载或进行镜像,否则将追究法律责任。