1.简介
web框架,快速开发网站,提供内置服务器
2.环境搭建
本教程系列基于python 2.7和django 1.11.6
2.1下载地址:
python 2.7
https://www.python.org/downloads/release/python-2716/
django 1.11.6
https://github.com/django/django/releases?after=2.0
使用pip命令安装无需下载django
2.2 环境
系统变量-path,删除之前python3的环境变量
C:\sof\python27
C:\sof\python27\scripts
测试:
命令行输入python
安装django:
pip install Django=1.11.6
3 hello world程序
3.1 新建项目


文件夹解释
djangotest 项目根路径
djangotest/djangotest 项目包
templates 所有前端页面信息
settings.py 数据库连接信息/模板配置/应用配置等
urls.py 映射路径(路由)
wsgi.py 网关接口(无需配置)
manage.py 管理程序(启动)
3.2 创建视图文件
views.py
# coding=utf-8
from django.http import HttpResponse
# 显示 helloworld
def index_view(request):
return HttpResponse('hello world')
3.3 配置路由
urls.py
import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^hello/', views.index_view)
]
3.4 运行:
python manage.py runserver 127.0.0.1:8000
浏览器访问:
http://127.0.0.1:8000/hello/
浏览器显示 hello world
说明:
光标移至index_view,alt+回车可创建views.py的方法
urls中使用中文注释会报错
修改端口:

网友评论