簡(jiǎn)要記錄步驟與操作,快速實(shí)現(xiàn),方便自己以后再開發(fā)時(shí)來看。
系統(tǒng)環(huán)境
- 操作系統(tǒng):MAC macOS Sierra version:10.12.1
- IDE:PyCharm 5.0.3
- 數(shù)據(jù)庫(kù):SQLlite
- Django1.10
步驟:
創(chuàng)建項(xiàng)目
啟動(dòng)PyCharm,新建一個(gè)Django項(xiàng)目,會(huì)自動(dòng)應(yīng)用最新版本
在一個(gè)新建空文件夾中新建項(xiàng)目,選擇啟動(dòng)自帶管理后臺(tái),項(xiàng)目名MySpace
修改Django設(shè)置
MySpace/settings.py
#系統(tǒng)語言設(shè)置為簡(jiǎn)體中文
LANGUAGE_CODE = "zh-hans"
#修改時(shí)間設(shè)置
TIME_ZONE = "Asia/Shanghai"
初始化數(shù)據(jù)表
$ python manage.py migrate
要先輸入該命令,否則無法創(chuàng)建用戶
創(chuàng)建超級(jí)管理員
$ python manage.py createsuperuser
根據(jù)提示輸入用戶名稱和密碼,注意密碼要求至少八位,不能全部數(shù)字
啟動(dòng)服務(wù)查看效果
$ python manage.py runserver
服務(wù)啟動(dòng)后,默認(rèn)地址為http://127.0.0.1:8000
可以看到界面顯示系統(tǒng)已正常運(yùn)行
訪問http://127.0.0.1:8000/admin
可以訪問系統(tǒng)后臺(tái)
創(chuàng)建第一應(yīng)用——welcome作為系統(tǒng)首頁管理模塊
創(chuàng)建第一個(gè)應(yīng)用用于管理系統(tǒng)首頁
$ python manage.py startapp welcome
在templats中新建html文件
在templats中新建html文件,index.html作為系統(tǒng)首頁,隨意在<body>后輸入內(nèi)容。
在welcome的視圖中定義引用index
welcome/views.py
def index(request)
return render(request, 'index.html')
在settings.py中注冊(cè)應(yīng)用
/MySpace/settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'welcome',
]
在urls.py中增加地址記錄
...
from welcome.view import index
urlpatterns = [
...
#通過url(r'', index),指定系統(tǒng)首頁為index
url(r'', index),
]
再次啟動(dòng)服務(wù),訪問http://127.0.0.1:8000/ ,即可訪問剛剛創(chuàng)建的index界面
創(chuàng)建備忘錄(note)功能,用于記錄事項(xiàng)
備忘錄具有分組功能,每個(gè)備忘有題目和內(nèi)容兩個(gè)屬性
創(chuàng)建應(yīng)用note
$ python manage.py startapp note
為note創(chuàng)建模型
創(chuàng)建Note模型和NoteGroup模型,Note對(duì)象具有三個(gè)屬性,note_title(題目)、note_context(內(nèi)容),分類;NoteGroup有一個(gè)屬性note_group(分組名稱)
編輯note/models.py
note/models.py
...
class NoteGroup(models.Model):
note_group = models.CharField("標(biāo)題", max_length=200)
def __str__(self):
return self.note_group
##PyCharm中兩個(gè)類之間如果不空兩行,會(huì)有提示
class Note(models.Model):
note_class = models.ForeignKey(NoteGroup)
note_title = models.CharField("標(biāo)題", max_length=200)
note_context = models.CharField(max_length=4000)
def __str__(self):
return self.note_title
讓note應(yīng)用在管理站點(diǎn)中可編輯
note/admin.py
from django.contrib import admin
from .models import NoteGroup, Note
admin.site.register(NoteGroup)
admin.site.register(Note)
為模型定義中文名稱,方便查看
note/models.py
...
class NoteGroup(models.Model):
note_group = models.CharField("標(biāo)題", max_length=200)
def __str__(self):
return self.note_group
class Meta:
verbose_name = ('分組')
class Note(models.Model):
note_class = models.ForeignKey(NoteGroup)
note_title = models.CharField("標(biāo)題", max_length=200)
note_context = models.CharField(max_length=4000)
def __str__(self):
return self.note_title
class Meta:
verbose_name = ('列表')
使用第三方后臺(tái)
$ pip install bootstrap-admin
INSTALLED_APPS = [
'bootstrap_admin', #一定要放在`django.contrib.admin`前面
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'welcome',
'note',
'blog',
'article',
]
BOOTSTRAP_ADMIN_SIDEBAR_MENU = True