Django 學習筆記 第一堂: 環境建置與框架特色
安裝 django 環境
# 基本套件安裝 ,先檢查有沒有裝 python3 & pip3
$python3 -V
$sudo apt install python3-pip
# 已安裝套件查詢
$pip3 list
Using Django inside a Python virtual environment
Install the tool using pip3:
$sudo pip3 install virtualenvwrapper
# 加入 .bashrc
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
export VIRTUALENVWRAPPER_VIRTUALENV_ARGS=' -p /usr/bin/python3 '
export PROJECT_HOME=$HOME/Devel
source /usr/local/bin/virtualenvwrapper.sh
# refresh env 變數
$source ~/.bashrc
Using a virtual environment
There are just a few other useful commands that you should know (there are more in the tool documentation, but these are the ones you'll use regularly):
- deactivate — Exit out of the current Python virtual environment
- workon — List available virtual environments
- workon name_of_environment — Activate the specified Python virtual environment
- rmvirtualenv name_of_environment — Remove the specified environment.
# 新創一個虛擬環境名稱
netsys@netsys [django_test] $mkvirtualenv my_django_environment
#開好之後狀態就會呈現 (my_django_environment),用用指令離開環境
(my_django_environment) netsys@netsys [django_test] $deactivate
#因為環境創過,之後就用 workon 去開
netsys@netsys [django_test] $workon my_django_environment
# 用 django-admin 開創一個 web
netsys@netsys$ mkdir django_test
netsys@netsys$ cd django_test
netsys@netsys [mytestsite]$ django-admin startproject mytestsite
### Django 網頁架構,可以用 tree 來檢視一下
netsys@netsys [django_test] $ tree ./mytestsite/
./mytestsite/
├── db.sqlite3
├── manage.py
└── mytestsite
├── __init__.py
├── __pycache__
│ ├── __init__.cpython-36.pyc
│ ├── settings.cpython-36.pyc
│ ├── urls.cpython-36.pyc
│ └── wsgi.cpython-36.pyc
├── settings.py
├── urls.py
└── wsgi.py
2 directories, 10 files
# 藉由更改 settings.py ALLOWED_HOSTS 的設定,允許 用public ip 連,不然他預設就是 127.0.0.1
ALLOWED_HOSTS = ['*']
#更改後就可以指定 ip 給他囉~(預設 port 8000)
$python manage.py runserver 140.113.220.167:8080
Performing system checks...
System check identified no issues (0 silenced).
You have 15 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. ### 如果出現以上 ,就執行 python manage.py migrate
December 16, 2018 - 07:06:30
Django version 2.1.5, using settings 'mytestsite.settings'
Starting development server at http://140.113.220.167:8080/
Quit the server with CONTROL-C .........(只要有和網頁互動就會有 console log post 出來~~~~)
因為我再vm有轉NAT指定某個port給django,所以固定用8000給他。
$python3 manage.py runserver 10.0.2.15:8000
留言
張貼留言