python django实例_python django 模板实例
操作系統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
{{ test }}
修改 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
總結
以上是生活随笔為你收集整理的python django实例_python django 模板实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: w10的产品密钥怎么查看 查看Windo
- 下一篇: 重装系统怎么分区 重装系统如何进行磁盘分