安装django1.10.2
python -m pip install django
修改环境变量PATH
d:\python36\Script;
查看版本号
python -m django --version
检查是否安装成功,cmd下运行
C:\Users\Administrator>python -m django
Type '__main__.py help <subcommand>' for help on a specific subcommand.
Available subcommands:
[django]
check
compilemessages
createcachetable
dbshell
diffsettings
dumpdata
flush
inspectdb
loaddata
安装pycharm用于开发python程序的ide
http://www.jetbrains.com/pycharm/download/download-thanks.html?platform=windows
----------------------------------------------------------------------------------
新建django项目
D:\workspace_python\mysite
项目的工程目录如下:
mysite/
mysite/
__init__.py
settings.py
urls.py
wsgi.py
templates
manage.py
------------------------------------
创建app(子模块)
在terminal控制台输入:
python manage.py startapp jkx
自动创建jkx文件夹
-------------------------------------
编写路由urls.py
from django.conf.urls import url
from django.contrib import admin
from jkx import views
urlpatterns = [
#admin后台路由
#url(r'^admin/', admin.site.urls),
#添加路由
url(r'index/',views.index)
]
----------------------------------------------
编写业务处理逻辑jkx/views.py
from django.shortcuts import render
from django.shortcuts import HttpResponse
# Create your views here.
def index(request):
return HttpResponse('Hello word')
-------------------------------------------------
运行web服务
#在不影响其他模块的情况下创建表结构
python manage.py migrate
#检查语法错误
python manage.py validate
#启动服务器
python manage.py runserver
检查运行结果:
http://localhost:8000/index/
------------------------------------------------------------------
返回html
在templates中添加index.html页面,添加如下内容:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>tese</title>
</head>
<h1 style="background-color: antiquewhite;color: black">hello world</h1>
</html>
修改view.py
from django.shortcuts import render
from django.shortcuts import HttpResponse
# Create your views here.
def index(request):
# return HttpResponse('Hello word')
return render(request, 'index.html')
-----------------------------------------------------------------
添加静态文件
在mysite下新建static目录
其他插件也可以放到该目录下
js
imgs
css
plugins
需要在settings.py文件,配置新添加的静态文件
STATIC_URL = '/static/'
STATICFILES_DIRS=(
os.path.join(BASE_DIR,'static'),
)
页面中引用js文件
<script src="/static/js/jquery-3.1.1.js"></script>
------------------------------------------------
接收用户发送数据
<!DOCTYPE html>
<html>
<head>
<meta charset="utf
python -m pip install django
修改环境变量PATH
d:\python36\Script;
查看版本号
python -m django --version
检查是否安装成功,cmd下运行
C:\Users\Administrator>python -m django
Type '__main__.py help <subcommand>' for help on a specific subcommand.
Available subcommands:
[django]
check
compilemessages
createcachetable
dbshell
diffsettings
dumpdata
flush
inspectdb
loaddata
安装pycharm用于开发python程序的ide
http://www.jetbrains.com/pycharm/download/download-thanks.html?platform=windows
----------------------------------------------------------------------------------
新建django项目
D:\workspace_python\mysite
项目的工程目录如下:
mysite/
mysite/
__init__.py
settings.py
urls.py
wsgi.py
templates
manage.py
------------------------------------
创建app(子模块)
在terminal控制台输入:
python manage.py startapp jkx
自动创建jkx文件夹
-------------------------------------
编写路由urls.py
from django.conf.urls import url
from django.contrib import admin
from jkx import views
urlpatterns = [
#admin后台路由
#url(r'^admin/', admin.site.urls),
#添加路由
url(r'index/',views.index)
]
----------------------------------------------
编写业务处理逻辑jkx/views.py
from django.shortcuts import render
from django.shortcuts import HttpResponse
# Create your views here.
def index(request):
return HttpResponse('Hello word')
-------------------------------------------------
运行web服务
#在不影响其他模块的情况下创建表结构
python manage.py migrate
#检查语法错误
python manage.py validate
#启动服务器
python manage.py runserver
检查运行结果:
http://localhost:8000/index/
------------------------------------------------------------------
返回html
在templates中添加index.html页面,添加如下内容:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>tese</title>
</head>
<h1 style="background-color: antiquewhite;color: black">hello world</h1>
</html>
修改view.py
from django.shortcuts import render
from django.shortcuts import HttpResponse
# Create your views here.
def index(request):
# return HttpResponse('Hello word')
return render(request, 'index.html')
-----------------------------------------------------------------
添加静态文件
在mysite下新建static目录
其他插件也可以放到该目录下
js
imgs
css
plugins
需要在settings.py文件,配置新添加的静态文件
STATIC_URL = '/static/'
STATICFILES_DIRS=(
os.path.join(BASE_DIR,'static'),
)
页面中引用js文件
<script src="/static/js/jquery-3.1.1.js"></script>
------------------------------------------------
接收用户发送数据
<!DOCTYPE html>
<html>
<head>
<meta charset="utf