1.建立django项目
django-admin startproject myblog
2.进入myblog目录 建立app存储自己的基本信息
python manage.py startapp person
3.在settings.py中注册app,在INSTALLED_APPS里面加入'pseron'
4.在settings.py中设置时间为中国时间:TIME_ZONE = 'Asia/Shanghai'
5.在settings.py中设置templates的路径:在TEMPLATES中设置'DIRS': [os.path.join(BASE_DIR,'templates')],并在项目下新建一个templates文件夹
6.在settings.py中设置静态文件static的路径:加入STATICFILES_DIRS=[os.path.join(BASE_DIR,'static'),],并在项目下新建一个static文件夹
7.在settings.py中设置连接mysql数据库:配置DATABASES为:
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'django', 'USER':'root', 'PASSWORD':'123456', 'HOST':'localhost', 'PORT':3306, } }
并在myblog下的__init__.py配置
import pymysql pymysql.install_as_MySQLdb()
8.在person目录下新建urls.py
9.总体目录为下:
10.整体流程的检测。
(1)myblog/urls.py
from django.contrib import admin from django.urls import path,include urlpatterns = [ path('admin/', admin.site.urls), path('',include('person.urls',namespace='person')) ]
(2)person/urls.py
from django.urls import path from . import views app_name='person' urlpatterns=[ path('person/',views.index,name='index') ]
(3)views.py
from django.shortcuts import render # Create your views here. def index(request): return render(request,'person/index.html')
(4)在templates下新建person文件夹,建立index.html
{% load staticfiles %} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href={% static 'person/css/index.css'%}> </head> <body> <h1 class="test">hello world</h1> </body> </html>
(5)在static下新建person文件夹,新建css文件夹,建立index.css
.test{
color: aqua;
}
11.启动服务器
python manage.py runserver
出现以下界面,则配置基本没有错。
12.在浏览器输入
http://127.0.0.1:8000/person/
得到以下结果:
至此,我们初步的框架就搭建而成了。