-
安装python3以上版本
傻瓜式安装直接下一步下一步...有勾选的尽量都勾上(pip包管理记得必须勾上方便后面安装其他需要的包)
-
安装 django
在cmd中输入: python pip install django==2.1.1
Djago后面的版本号不写的话默认安装最新版本(目前最新版本是2.2)
可输入如下指令查看安装是否成功
<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1555137950364 ql-align-center" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; text-align: left; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"><input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>
- 创建一个项目
Cmd中输入: python django-admin startproject mysite
其中mysite为项目名称可以自己随便取名字 如果没有用cd命令进入某个文件夹的话mysite项目文件会被创建到默认的系统用户路径下
<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1555137950373 ql-align-center" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; text-align: left; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"><input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>
Mysuie项目文件下会自动默认创建上图文件(其中blog learn myblog mysecondweb 文件夹是我后面创建的应用程序目录)
4.创建应用
在cmd中使用cd命令进入上一步创建的mysite 目录
再输入: mysite python manage.py startapp myblog
即可创建名为 myblog的应用
5.博客应用的表设计
在myblog目录下model.py中编辑写:
from django.db import models
from django.utils import timezone
# Create your models here.
class Category(models.Model):
*"""文章分类"""*
name = models.CharField(verbose_name='文章类别', max_length=20)
**class** Meta:
verbose_name = **'文章类别'**
verbose_name_plural = verbose_name
**def** __str__(self):
**return** self.name
class Tag(models.Model):
*""" 文章标签 """*
name = models.CharField(verbose_name='文章标签', max_length=20)
**class** Meta:
verbose_name = **'文章标签'**
verbose_name_plural = verbose_name
**def** __str__(self):
**return** self.name
class Blog(models.Model):
*""" 博客 """*
title = models.CharField(verbose_name='标题', max_length=100)
content = models.TextField(verbose_name=**'正文'**, default=**''**)
create_time = models.DateTimeField(verbose_name=**'创建时间'**, default=timezone.now)
modify_time = models.DateTimeField(verbose_name=**'修改时间'**, auto_now=**True**)
click_nums = models.IntegerField(verbose_name=**'点击量'**, default=0)
category = models.ForeignKey(Category, verbose_name=**'文章类别'**, on_delete=models.CASCADE)
tag = models.ManyToManyField(Tag, verbose_name=**'文章标签'**)
**class** Meta:
verbose_name = **'我的博客'**
verbose_name_plural = verbose_name
**def** __str__(self):
**return** self.title
- 激活应用
在mysite目录下settings.py中激活应用 myblog
<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1555137950413 ql-align-center" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; text-align: left; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"><input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>
7.生成数据库迁移文件
在cmd中输入: python manage.py makemigrations myblog
后面指定的myblog可省略
8.执行数据迁移
python manage.py migrate
<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1555137950418" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"><input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>
- 创建管理员
python manage.py createsuperuser
按回车后输入管理员信息
Python学习群:683380553,有大牛答疑,有资源共享!有想学习python编程的,或是转行,或是大学生,还有工作中想提升自己能力的,正在学习的小伙伴欢迎加入学习。
- 启动服务
python manage.py runserver
在浏览器中输入http://127.0.0.1:8000/admin即可打开后台登录界面
11. 将post模型注册到后台
编辑myblog目录下的admin.py 写下如图代码
<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1555137950428 ql-align-center" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; text-align: left; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"><input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>
- 登录网址后
<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>
创建完整的博客 未完待续。。。
网友评论