安装模块
pip install django==2.1.14
创建项目
django-admin startproject 项目名称 (例如,我写的是mysite)
django-admin startproject mysite
创建应用
cd 项目名称 (到指定文件夹目录下)
cd mysite
python manage.py startapp 应用名称 (在这里我注册了 app01)
python manage.py startapp app01
配置文件settings.py
INSTALL_APPS = [
....
'应用名称'
]
MIDDLEWARE = [
....
# 'django.middleware.csrf.CsrfViewMiddleware' 注释
]
DATABASE = [
'default':{
'ENGINE':'django.db.backends.mysql',
'NAME':'数据库名称',
'HOST':'127.0.0.1',
'PORT':3306,
'USER':'数据库用户名',
'PASSWORD':'数据库密码'
}
]
LANGUAGE_CODE = 'zh-hans'
TIME_ZONE = 'Asia/Shanghai'
USE_TZ = False
更改 项目名/__init__.py
import pymysql
pymysql.install_as_Mysqldb()
一级路由 urls.py 包含 include
from django.contrib import admin
from django.urls import path,include
# 在路由输入 'index/',会打印出 'hello world !!!'
def index(requset):
return HttpResponse("hello world !!!")
# 一级路由
urlpatterns = [
path('admin/', admin.site.urls),
path('index/',index),
path('app01/', include('app01.urls')),
# 这里的app01是上面创建的应用名字
]
二级路由 (应用下的路由文件)
from django.urls import path
from . import views
# 二级路由
urlpatterns = [
path('hello/',views.hello)
]
应用下的 views.py
# 导包
from django.http import HttpResponse
# 创建index方法 (先输入一级路由/接二级路由;会打印出"hello world!!! hello python!!!")
# 若还是不明白,在下面会详细解说
def hello(request):
return HttpResponse('Hello world!!! hello python!!!')
启动服务
python manage.py runserver
Hello world!!!
Hello world!!! Hello python!!!
原文:https://www.cnblogs.com/chao460/p/12091294.html