學習筆記,僅供參考
本系列Blog以應用爲主,理論基礎部分我在後端專欄的Django系列博客已經寫過了,如果有些需要補充的知識點,我會在這個系列中,儘量詳細的寫一下。
創建一個Django項目
設置虛擬環境
- 進入虛擬環境
workon mymkvir
- 查看虛擬環境下的包
pip list
#輸出
Package Version
---------- -------
Django 2.2.13
pip 20.1.1
pytz 2020.1
setuptools 47.3.1
sqlparse 0.3.1
wheel 0.34.2
創建項目
- 創建Django項目(newwebsite1)
進入要存放項目的目錄:
cd F:\MyStudio\PythonStudio\goatbishop.project01\Django
創建一個項目:
django-admin startproject [項目名稱]
#比如
django-admin startproject newwebsite1
- 進入新創建的項目(newwebsite1)
進入項目文件:
cd newwebsite1
查看項目文件夾下的文件:
dir
#輸出
2020/07/01 12:16 <DIR> .
2020/07/01 12:16 <DIR> ..
2020/07/01 12:16 652 manage.py
2020/07/01 12:16 <DIR> newwebsite1
1 個文件 652 字節
3 個目錄 26,557,247,488 可用字節
- 運行該項目
python manage.py runserver
#輸出
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
You have 17 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
July 01, 2020 - 12:34:28
Django version 2.2.13, using settings 'newwebsite1.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
默認監聽8000端口。
- 在瀏覽器中向http://127.0.0.1:8000/發起請求
- 指定端口號爲5000開啓服務
python manage.py runserver 5000
#輸出
Performing system checks...
System check identified no issues (0 silenced).
You have 17 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
July 01, 2020 - 12:39:53
Django version 2.2.13, using settings 'newwebsite1.settings'
Starting development server at http://127.0.0.1:5000/
Quit the server with CTRL-BREAK.
- 更改ip地址
如果我們網站的IP地址爲127.0.0.1,那麼只有我們自己的電腦可以訪問這個項目,別人是訪問不到的,如果我想要別人(同一個局域網下)也可以訪問,就需要指定IP地址爲0.0.0.0
python manage.py runserver 0.0.0.0:8000
在項目文件夾下的settings.py中修改允許訪問的域名ALLOWED_HOSTS:
ALLOWED_HOSTS = ['*']
#或者是別人的ip地址
這樣別人的電腦就可以訪問我們的項目了(注意要關閉防火牆)。
查看manage.py中還有哪些子命令
python manage.py help
#輸出
[auth]
changepassword
createsuperuser
[contenttypes]
remove_stale_contenttypes
[django]
check
compilemessages
createcachetable
dbshell
diffsettings
dumpdata
flush
inspectdb
loaddata
makemessages
makemigrations
migrate
sendtestemail
shell
showmigrations
sqlflush
sqlmigrate
sqlsequencereset
squashmigrations
startapp
startproject
test
testserver
[sessions]
clearsessions
[staticfiles]
collectstatic
findstatic
runserver