----认识Django:
Django是一个开放源代码的webMVC框架
M:entity framework
V:view.py
C:urls.py
-------一览Django结构:
manage.py admin tool:项目的管理员工具
application 应用层
urls.py 路由设置 (网址入口,关联到对应的views.py中的一个函数(或者generic类),访问网址就对应一个函数。)
settings.py project设置(Django 的设置,配置文件,比如 DEBUG 的开关,静态文件的位置等)
wsgi.py web server grate interface (类似tomcat)
views.py controller(处理用户发出的请求,从urls.py中对应过来, 通过渲染templates中的网页可以将显示内容)
models.py entity framework
执行流程:访问地址栏-》urls.py(通过地址栏,调用其对应的views中的方法)-》views.py(调用方法后返回指定的页面)
----略过Django的安装
我们开始搭建第一个Django项目吧!
-----------华丽的分割线
---具体操作
manage.py startapp student :创建student application
手动添加一个静态资源的目录html
配置settings.py TEMPLATES==>DIRS
在html下创建一个hello.html
编写views.py
def hello(request):
return render(request,"hello.html") # Response
配置路由urls.py
from student import views as studentViews
url(r'^student/hi$', studentViews.hello),
---结构如图
-----具体代码
--urls.py
from django.conf.urls import url from django.contrib import admin from student import views as studentViews urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^student/reg$', studentViews.reg), url(r'^student/hello$', studentViews.hello), ]
--views.py
from django.shortcuts import render # Create your views here. def hello(request): return render(request,"hello.html",{"userName":request.GET["userName"]}) def reg(request): return render(request,"reg.html")
--reg.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form method="get" action="hello"> uName:<input name="userName" id="userName" type="text"/><br/> <input type="submit" value="注册">| <input type="reset" value="取消"> </form> </body> </html>
--hello.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>hello,django.{{userName}} <h1> </body> </html>
---------ok,django小demo完成了