橡皮擦兴革,一個(gè)逗趣的互聯(lián)網(wǎng)高級(jí)網(wǎng)蟲(chóng)绎晃。新的系列蜜唾,讓我們一起進(jìn)入 Django 世界。
二庶艾、Python Django 微型博客
通過(guò)簡(jiǎn)單的操作袁余,就可以在 Django 中實(shí)現(xiàn)一個(gè)博客 應(yīng)用
2.1 用模板生成文件
進(jìn)入到 上篇博客 創(chuàng)建的目錄中,執(zhí)行下述命令咱揍。
django-admin startapp blog
# 也可以使用下述命令
python manage.py startapp blog
命令運(yùn)行之后颖榜,生成的目錄如下。
相關(guān)文件說(shuō)明如下:
blog # 根目錄
__init__.py
admin.py # 后臺(tái)管理
apps.py # 應(yīng)用設(shè)置相關(guān)
models.py # 模型煤裙,數(shù)據(jù)庫(kù)相關(guān)
tests.py # 測(cè)試相關(guān)
views.py # 視圖相關(guān)
migrations # 數(shù)據(jù)庫(kù)變更記錄文件夾
接下來(lái)修改 my_website
文件夾中的 settings.py
文件掩完,添加 blog
應(yīng)用,具體代碼如下硼砰,重點(diǎn)是最后一行且蓬。
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog', # 這里是非常重要的
]
在 Django 中,如果希望應(yīng)用模型题翰、靜態(tài)文件恶阴、模板這些內(nèi)容,需要在 INSTALLED_APPS
添加對(duì)應(yīng)的應(yīng)用目錄豹障,如果沒(méi)有設(shè)置冯事,應(yīng)用不會(huì)進(jìn)行加載。
2.2 創(chuàng)建文章模型與數(shù)據(jù)庫(kù)結(jié)構(gòu)
一篇博客包括標(biāo)題血公,內(nèi)容昵仅,發(fā)布時(shí)間等內(nèi)容,這些都在 Django 中進(jìn)行創(chuàng)建坞笙,核心要修改的是 models.py
文件岩饼。
from django.db import models
# Create your models here.
class Blog(models.Model):
title = models.CharField("標(biāo)題", max_length=100)
content = models.TextField("內(nèi)容")
creatr_time = models.DateField("發(fā)布時(shí)間")
該類(lèi)就是模型類(lèi),實(shí)現(xiàn)一個(gè)模型類(lèi)薛夜,需要繼承自 models.Model
類(lèi),該類(lèi)用于進(jìn)行數(shù)據(jù)庫(kù)中數(shù)據(jù)和 Python 對(duì)象之間的轉(zhuǎn)換操作版述。
下面將 Blog 類(lèi)反射到 sqlite3
數(shù)據(jù)庫(kù)中梯澜,本階段請(qǐng)以模仿為主,后續(xù)會(huì)為你補(bǔ)充完整相應(yīng)的知識(shí)框架渴析。
在 manage.py
所在的文件夾中運(yùn)行下述命令:
python manage.py makemigrations blog
該命令用于檢查 blog
中模型文件的變更晚伙,由于在上文我們創(chuàng)建了一個(gè) Blog 類(lèi),所以 models.py
文件被修改俭茧,當(dāng)執(zhí)行上述命令時(shí)咆疗,會(huì)輸出如下內(nèi)容。
>python manage.py makemigrations blog
Migrations for 'blog':
blog\migrations\0001_initial.py
- Create model Blog
提示的信息也為 Create model Blog
母债,提示創(chuàng)建了一個(gè) Blog
類(lèi)午磁,下面將 models.py
中的內(nèi)容在 sqlite3
創(chuàng)建出來(lái)尝抖,使用的命令為 python manage.py migrate blog
,運(yùn)行結(jié)果如下:
> python manage.py migrate blog
Operations to perform:
Apply all migrations: blog
Running migrations:
Applying blog.0001_initial... OK
2.3 創(chuàng)建管理后臺(tái)
下面通過(guò)簡(jiǎn)單的步驟實(shí)現(xiàn)對(duì) blog 表的管理迅皇,Django 自帶用戶(hù)認(rèn)證系統(tǒng)昧辽,通過(guò)命令 python manage.py migrate
即可實(shí)現(xiàn)。
> python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, blog, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying admin.0003_logentry_add_action_flag_choices... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying auth.0009_alter_user_last_name_max_length... OK
Applying auth.0010_alter_group_name_max_length... OK
Applying auth.0011_update_proxy_permissions... OK
Applying auth.0012_alter_user_first_name_max_length... OK
Applying sessions.0001_initial... OK
該命令創(chuàng)建一些列的數(shù)據(jù)庫(kù)表結(jié)構(gòu)登颓,表結(jié)構(gòu)出現(xiàn)之后搅荞,還需要?jiǎng)?chuàng)建登錄賬號(hào),用到的核心命令是 python manage.py createsuperuser
框咙。
> python manage.py createsuperuser
Username (leave blank to use 'administrator'): xiangpica
Email address: dream@163.com
Password:
Password (again):
The password is too similar to the username.
Bypass password validation and create user anyway? [y/N]: y
Superuser created successfully.
準(zhǔn)備工作實(shí)現(xiàn)完畢咕痛,對(duì) admin.py
文件進(jìn)行修改,注意 admin.py
文件的位置以及模塊之間的調(diào)用關(guān)系喇嘱。
from django.contrib import admin
from blog.models import Blog
# Register your models here.
@admin.register(Blog)
class BlogAdmin(admin.ModelAdmin):
pass
在正式運(yùn)行之前茉贡,還需要修改一下網(wǎng)頁(yè)展示中英文內(nèi)容,打開(kāi) setting.py
文件婉称,修改如下內(nèi)容块仆。
# LANGUAGE_CODE = 'en-us'
LANGUAGE_CODE = 'zh-hans'
準(zhǔn)備工作完成,控制臺(tái)運(yùn)行下述命令 python manage.py runserver
:
> python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
March 24, 2021 - 22:22:33
Django version 3.1.7, using settings 'my_website.settings'
Starting development server at http://127.0.0.1:8000/
此時(shí)打開(kāi)默認(rèn)頁(yè)面王暗,展示的內(nèi)容如下:
修改訪問(wèn)地址為:http://127.0.0.1:8000/admin悔据,出現(xiàn)登錄窗口,使用上文注冊(cè)的賬號(hào)即可訪問(wèn)俗壹。
輸入一些文章相關(guān)信息進(jìn)行保存之后科汗,返回列表頁(yè)面,出現(xiàn)一個(gè)小細(xì)節(jié)绷雏,橙色框內(nèi)的數(shù)據(jù)是一個(gè) Blog object
头滔,不是文章的標(biāo)題,繼續(xù)修改代碼涎显。
修改 modes.py
文件坤检,修改內(nèi)容如下:
from django.db import models
# Create your models here.
class Blog(models.Model):
title = models.CharField("標(biāo)題", max_length=100)
content = models.TextField("內(nèi)容")
creatr_time = models.DateField("發(fā)布時(shí)間")
def __str__(self):
return self.title
一個(gè)小型的博客完成,你可以對(duì)文章進(jìn)行編輯與刪除操作期吓。
相關(guān)閱讀
- Python 爬蟲(chóng) 100 例教程早歇,超棒的爬蟲(chóng)教程,立即訂閱吧
- Python 游戲世界(更新中讨勤,目標(biāo)文章數(shù) 50+箭跳,現(xiàn)在訂閱,都是老粉)
- Python 爬蟲(chóng)小課潭千,精彩 9 講
今天是持續(xù)寫(xiě)作的第 <font color="red">118</font> / 200 天谱姓。
如果你想跟博主建立親密關(guān)系,可以關(guān)注同名公眾號(hào) <font color="red">夢(mèng)想橡皮擦</font>刨晴,近距離接觸一個(gè)逗趣的互聯(lián)網(wǎng)高級(jí)網(wǎng)蟲(chóng)屉来。
博主 ID:夢(mèng)想橡皮擦路翻,希望大家<font color="red">點(diǎn)贊</font>、<font color="red">評(píng)論</font>奶躯、<font color="red">收藏</font>帚桩。