操作系統(tǒng)MAC OS/LINUX
windows
安裝python3
版本3自帶virtualenv
安裝virtualenv: pip install virtualenv
創(chuàng)建虛擬環(huán)境
virtualenv env1
進(jìn)入虛擬環(huán)境
mac os/linux下: source env1/bin/activate
window下:source env1/Scripts/activate
安裝django
pip3 install django~=2
創(chuàng)建項(xiàng)目
django-admin startproject test_template
#python version 3.7
#django version 2.0
└── test_templates
├── manage.py
└── test_templates
├── __init__.py
├── settings.py
├── urls.py
└── wsgi.py
創(chuàng)建數(shù)據(jù)庫
python3 manage.py makemigrations
python3 manage.py migrate
python3 manage.py createsuperuser
#python version 3.7
#django version 2.0
.
└── test_templates
├── db.sqlite3
├── manage.py
└── test_templates
├── __init__.py
├── __pycache__
│ ├── __init__.cpython-37.pyc
│ ├── settings.cpython-37.pyc
│ └── urls.cpython-37.pyc
├── settings.py
├── urls.py
└── wsgi.py
創(chuàng)建模板實(shí)例
新建templates, templates/test.html
新建test_templates/view.py
#python version 3.7
#django version 2.0
└── test_templates
├── db.sqlite3
├── manage.py
├── templates
│ └── test.html
└── test_templates
├── __init__.py
├── __pycache__
│ ├── __init__.cpython-37.pyc
│ ├── settings.cpython-37.pyc
│ ├── urls.cpython-37.pyc
│ ├── view.cpython-37.pyc
│ └── wsgi.cpython-37.pyc
├── settings.py
├── urls.py
├── view.py
└── wsgi.py
修改test_templates/view.py
from django.shortcuts import render
def test(request):
context = {}
context['test'] = 'test!!!!'
return render(request, 'test.html', context)
修改template/test.html
<h1>{{ test }}</h1>
修改 test_templates/settings.py
...
TEMPLATES = [
{
...
'DIRS': [BASE_DIR + '/templates']
...
}
]
修改test_templates/urls.py
from django.conf.urls import url
from . import view
urlpatterns = [
url(r'^$', view.test)
]
啟動(dòng)應(yīng)用
python3 manage.py runserver 0.0.0.0:8080