操作系统MAC OS/LINUX
windows
安装python3
版本3自带virtualenv
安装virtualenv: pip install virtualenv
创建虚拟环境
virtualenv env1
进入虚拟环境
mac os/linux下: source env1/bin/activate
window下:source env1/Scripts/activate
安装django
pip3 install django~=2
创建项目
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
创建数据库
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
创建模板实例
新建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)
]
启动应用
python3 manage.py runserver 0.0.0.0:8080
网友评论