标签:close .com 上传 delete sed direct 规则 clu start

-->url对应关系(匹配) ->视图函数->返回用户字符串
-->url对应关系(匹配)->视图函数->打开一个HTML文件,读取内容
创建django project
django-admin startproject mysite
cd mysite
python manage.py startapp cmdb
mysite
mysite
--配置文件
-url.py
-settings.py
cd mysite
cmdb
-views.py
-admin.py
-models.py # 创建数据库表
3、配置
模板路径
静态文件路径
注释 #csrf
4、编写程序
a.url.py 写对应关系 /index/ -> func
b. views.py
def func(request): # request包含所有的请求数据
...业务代码
return HttpResponse(‘字符串‘)
return render(request,‘index.html‘,{‘‘})
return redirect(‘url‘)
c.模板语言
{% for item in %}
一、路由系统,url
二、视图
request.GET
request.POST
request.FILES
#checkbox等多选的内容 request.POST.getlist()
#上传文件
obj = request.FILES.get(‘name‘)
obj.name
f = open(obj.name,mode=‘wb‘)
for item in obj.chunks():
f.write(item)
f.close()
views.py
from django.shortcuts import render,HttpResponse,redirect
# Create your views here.
def index(request):
return HttpResponse(‘Index‘)
‘‘‘
def login(request):
if request.method == ‘GET‘:
return render(request,‘login.html‘)
elif request.method == ‘POST‘:
u = request.POST.get(‘user‘)
p = request.POST.get(‘pwd‘)
if u == ‘alex‘ and p == ‘123‘:
return redirect(‘/index/‘)
else:
return redirect(‘login.html‘)
else:
# PUT,DELETE,HEAD,OPTION...
return redirect(‘/index/‘)
‘‘‘
def login(request):
if request.method == ‘GET‘:
return render(request,‘login.html‘)
elif request.method == ‘POST‘:
v = request.POST.get(‘gender‘)
print(v)
f = request.POST.getlist(‘favor‘) # 获取checkbox的多人值
print(f)
fff = request.FILES.get(‘file‘) # 默认只拿到文件名
content = request.FILES.get(‘file‘) # 拿文件内容
print(fff,type(fff))
return render(request,‘login.html‘)
else:
# PUT,DELETE,HEAD,OPTION...
return redirect(‘/index/‘)
#上传文件的话,form标签要做特殊处理 enctype="multipart/form-data"
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="/login/" method="post" enctype="multipart/form-data"> <p> <input type="text" name="user" placeholder="用户名"> </p> <p> <input type="password" name="pwd" placeholder="密码"> </p> <p> 男:<input type="radio" name="gender" value="1"> 女:<input type="radio" name="gender" value="2"> 张杨:<input type="radio" name="gender" value="3"> </p> <p> 男:<input type="checkbox" name="favor" value="11"> 女:<input type="checkbox" name="favor" value="22"> 张杨:<input type="checkbox" name="favor" value="33"> </p> <p> <select multiple> <option value="sh">上海</option> <option value="bj">北京</option> <option value="tj">天津</option> </select> </p> <p> <input type="file" name="file"> </p> <input type="submit" value="提交"> </form> </body> </html>
login.html
Django路由规则
1、基于正则的URL
在templates目录下创建index.html、detail.html文件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> {% for k,item in user_dict.items %} <li><a target="_blank" href="/detail-{{ k }}.html">{{ item.name}}</a></li> {% endfor %} </body> </html> index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h2>详细信息</h2> <h4>用户名:{{ detail_info.name }}</h4> <h4>邮箱:{{ detail_info.email }}</h4> </body> </html> detail.html
在urls.py文件增加对应路径
from django.conf.urls import url,include
from django.contrib import admin
from cmdb import views
urlpatterns = [
url(r‘^index‘, views.index),
url(r‘^detail-(\d+).html/‘, views.detail),
]
标签:close .com 上传 delete sed direct 规则 clu start
原文地址:http://www.cnblogs.com/ld1977/p/6964997.html