安装:html
pip install Django
目录添加到系统环境变量中: C:\Python34\Lib\site-packages\django;C:\Python34\Scriptspython
project:web
使用 django-admin 来建立 HelloWorld 项目:数据库
django-admin startproject HelloWorld
建立完成后咱们能够查看下项目的目录结构:django
HelloWorld/ #容器文件夹,由django建立的 |-- HelloWorld #主文件夹,由django建立的 | |-- __init__.py | |-- settings.py #该Django项目的设置/配置。 | |-- urls.py #路由 | `-- wsgi.py `-- manage.py #一个实用的命令行工具,可以让你以各类方式与该 Django 项目进行交互。 ———命令行使用
运行:app
python manage.py runserver 0.0.0.0:8000
——能够边运行,边修改框架
HelloWorld/HelloWorld/urls.py 函数
from django.urls import path from django.conf.urls import url from . import view,testdb,search,search2 urlpatterns = [ #path() url() 均可以,不一样的路由规则 path('view', view.rer), #显示html url(r'^testdb$', testdb.testdb), #数据库操做 path('dbGet', testdb.dbGet), path('dbUpdate', testdb.dbUpdate), path('dbDel', testdb.dbDel), url(r'^search-form$', search.search_form), #表单 url(r'^search$', search.search), url(r'^search-post$', search2.search_post), ]
HelloWorld/HelloWorld/view.py工具
from django.http import HttpResponse from django.shortcuts import render def hello(request): return HttpResponse("Hello world ! ") def rer(request): context = {} context['hello'] = 'rer' return render(request, 'hello.html', context) #渲染模板
HelloWorld/templates/hello.htmlpost
<h1>{{ hello }}</h1>
HelloWorld/HelloWorld/settings.py
INSTALLED_APPS = [ 'TestModel', # 添加app ... 'DIRS': ["HelloWorld"+"/templates",], # 修改模板文件夹 ... DATABASES = { #数据库配置 'default': {
app:
一个project能够包含多个app,一个app实现某个功能
HelloWorld> django-admin startapp TestModel
HelloWorld #容器文件夹
Your apps can live anywhere on your Python path. Generally, we’ll create our poll app right next to your manage.py file so that it can be imported as its own top-level module, rather than a submodule of mysite.
目录结构以下:
HelloWorld #容器文件夹,由django建立的 |-- TestModel #app1文件夹 | |-- __init__.py | |-- admin.py | |-- models.py | |-- tests.py | `-- views.py |-- HelloWorld #主文件夹 | `-- ... `-- manage.py # ———命令行使用
一个app能够有本身的views.py和urls.py
但必须在主文件夹的urls.py里面
urlpatterns = [ path('***', include('app1.urls')),
建立一个名为TestModel处理数据库的app
TestModel/models.py
from django.db import models class Test(models.Model): name = models.CharField(max_length=20)
在命令行中运行:
$ python manage.py migrate # 建立表结构 $ python manage.py makemigrations TestModel # 让 Django 知道咱们在咱们的模型有一些变动 $ python manage.py migrate TestModel # 建立表结构
看到几行 "Creating table…" 的字样,你的数据表就建立好了。
在 HelloWorld 目录中添加 testdb.py 文件 #主文件夹中
HelloWorld/HelloWorld/testdb.py
from django.http import HttpResponse from TestModel.models import Test # 数据库操做 def testdb(request): test1 = Test(name='runoob') test1.save() return HttpResponse("<p>数据添加成功!</p>") # 数据库操做 def dbGet(request): # 初始化 response = "" response1 = "" # 经过objects这个模型管理器的all()得到全部数据行,至关于SQL中的SELECT * FROM list = Test.objects.all() # filter至关于SQL中的WHERE,可设置条件过滤结果 response2 = Test.objects.filter(id=1) # 获取单个对象 response3 = Test.objects.get(id=1) # 限制返回的数据 至关于 SQL 中的 OFFSET 0 LIMIT 2; Test.objects.order_by('name')[0:2] #数据排序 Test.objects.order_by("id") # 上面的方法能够连锁使用 Test.objects.filter(name="runoob").order_by("id") # 输出全部数据 for var in list: response1 += var.name + " " response = response1 return HttpResponse("<p>" + response + "</p>") def dbUpdate(request): # 修改其中一个id=1的name字段,再save,至关于SQL中的UPDATE test1 = Test.objects.get(id=1) test1.name = 'Google' test1.save() # 另一种方式 #Test.objects.filter(id=1).update(name='Google') # 修改全部的列 # Test.objects.all().update(name='Google') return HttpResponse("<p>修改为功</p>") def dbDel(request): # 删除id=1的数据 test1 = Test.objects.get(id=1) test1.delete() # 另一种方式 # Test.objects.filter(id=1).delete() # 删除全部数据 # Test.objects.all().delete() return HttpResponse("<p>删除成功</p>")
/HelloWorld/HelloWorld/search.py
from django.http import HttpResponse from django.shortcuts import render # 表单 def search_form(request): return render(request,'search_form.html') # 接收请求数据 def search(request): request.encoding='utf-8' if 'q' in request.GET and request.GET['q']: message = '你搜索的内容为: ' + request.GET['q'] else: message = '你提交了空表单' return HttpResponse(message)
/HelloWorld/HelloWorld/search2.py
from django.shortcuts import render from django.views.decorators import csrf # 接收POST请求数据 def search_post(request): ctx ={} if request.POST: ctx['rlt'] = request.POST['q'] return render(request, "post.html", ctx)
在模板目录 templates 中添加 .html
/HelloWorld/templates/search_form.html
<!DOCTYPE html> <html><head><meta charset="utf-8"><title></title></head> <body> <form action="/search" method="get"> <input type="text" name="q"> <input type="submit" value="搜索"> </form> </body> </html>
/HelloWorld/templates/post.html
<!DOCTYPE html> <html><head><meta charset="utf-8"><title></title></head> <body> <form action="/search-post" method="post"> {% csrf_token %} <input type="text" name="q"> <input type="submit" value="Submit"> </form> <p>{{ rlt }}</p> </body> </html>
快云精简lamp就是django外面套层wsgi gunicorn是一个unix上被普遍使用的高性能的Python WSGI UNIX HTTP Server。
------------------------------------------------------------------------------
pip install web.py==0.40
import web #框架导入 import blog #嵌套的server_app ''' class blog: def GET(self, path): return "blog " + path app_blog = web.application(urls, locals()) ''' class index: def GET(self, path): return "hello " + path render = web.template.render('templates/', cache=False) class index2: def GET(self, code): web.header('Content-Type', 'text/xml') var=code return render.response(var) class RequestHandler(object): #read raw data from post def POST(self): data = web.data() # you can get a lot of data by the POST method urls = ( "/(.*)", "index" "/blog", blog.app_blog, #嵌套 "/index2", "index2" "/post","RequestHandler" ) #///Python 内置函数 #globals() 函数会以字典类型返回当前位置的全局变量。 #locals() 局部变量。 app = web.application(urls, globals()) app.run()