版权声明:本文为CSDN博主「一个平凡de人」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_46703850/article/details/121479796
django的安装和运行
- 安装django
[root@xunjian opt]# pip3 install django
- 创建一个项目
[root@xunjian opt]# django-admin startproject myDjango

- 目录说明:
myDjango: 项目的容器。
manage.py: 一个实用的命令行工具,可让你以各种方式与该 Django 项目进行交互。
init.py: 一个空文件,告诉 Python 该目录是一个 Python 包。
asgi.py: 一个 ASGI 兼容的 Web 服务器的入口,以便运行你的项目。
settings.py: 该 Django 项目的设置/配置。
urls.py: 该 Django 项目的 URL 声明; 一份由 Django 驱动的网站"目录"。
wsgi.py: 一个 WSGI 兼容的 Web 服务器的入口,以便运行你的项目。
4.启动服务器
配置 myDjango/settings.py
ALLOWED_HOSTS = ["192.168.2.22"]
[root@xunjian myDjango]# python3 manage.py runserver 0.0.0.0:8080

配置url路由
1.创建项目的Application
[root@xunjian myDjango]# python3 manage.py startapp sales

- 配置路由
[root@xunjian myDjango]# vim myDjango/urls.py
添加

- 修改views.py
[root@xunjian myDjango]# cat sales/views.py
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def listorders(request):
return HttpResponse("下面是系统中所有的订单信息。。。111")


路由子表
1.创建路由子表
[root@xunjian myDjango]# cat sales/urls.py
from django.urls import path
from sales.views import listorders,listorders2,listorders3
urlpatterns = [
# 添加如下的路由记录
path('1/', listorders),
path('2/', listorders2),
path('3/', listorders3),
]
2.修改主路由表
[root@xunjian myDjango]# cat myDjango/urls.py
from django.contrib import admin
from django.urls import path, include
#from sales.views import listorders
urlpatterns = [
path('admin/', admin.site.urls),
# 添加如下的路由记录
path('sales/', include('sales.urls')),
]
3.修改views.py
[root@xunjian myDjango]# cat sales/views.py
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def listorders(request):
return HttpResponse("下面是系统中所有的订单信息。。。111")
def listorders2(request):
return HttpResponse("下面是系统中所有的订单信息。。。222")
def listorders3(request):
return HttpResponse("下面是系统中所有的订单信息。。。333")
- 启动
[root@xunjian myDjango]# python3 manage.py runserver 0.0.0.0:8080



网友评论