- 构建项目
mkdir bysms && cd bysms
django-admin startproject config .
- config 包下的settings.py中编写如下
ALLOWED_HOSTS = ['127.0.0.1', 'localhost']
- 创建项目app
# 终端进入项目根路径,执行如下命令
python manage.py startapp sales
- sales 包下的views.py中编写如下
from django.http import HttpResponse
def listorders(request):
return HttpResponse("下面是系统中所有的订单信息。。。")
- config 包下的urls.py中编写如下
from django.contrib import admin
from django.urls import path
from sales.views import listorders
urlpatterns = [
path('admin/', admin.site.urls),
path('sales/orders/', listorders),
]
- 启动项目
python manage.py runserver 0.0.0.0:80
- 浏览器测试
-
路由子表
- sales 包下新建urls.py,编写如下
from django.urls import path
from . import views
urlpatterns = [
path('orders1/', views.listorders),
]
- 在config 包下的urls.py中编写如下
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
# 凡是 url 以 sales/ 开头的,
# 都根据 sales.urls 里面的 子路由表进行路由
path('sales/', include('sales.urls')),
]
- 访问时,先再config/urls中找到
sales/
,再去sales/urls.py中找到orders1/