第八章(1)
目录
- js正册表达式
- 提交事件顺序
- web模板
- Django
- Django内容整理
- Django请求生命周期
1.js正册表达式
test
匹配到返回true
rep = /\d+/
/\d+/
rep.test("asdsdf")
false
rep.test("asd12")
true
^$ 精确匹配
rep = /^\d+$/;
/^\d+$/
rep.test("112sdf")
false
rep.test("11")
true
exec
分组匹配和匹配所有(\W*) g text = "JavaScript is more fun than \nJava or JavaBeans!" var rep = /^Java(\W*)/g; rep.exec(text) 匹配所有行m text = "JavaScript is more fun than \nJava or JavaBeans!" var rep = /^Java(\W*)/gm; rep.exec(text)
2.提交时间顺序
-登录注册验证 默认事件先执行 checkbox 自定义事件先执行 a submit23 24
4.settings.py
1 TEMPLATES = [ 2 { 3 'BACKEND': 'django.template.backends.django.DjangoTemplates', 4 'DIRS': [os.path.join(BASE_DIR, 'templates')] #### 5 , 6 'APP_DIRS': True, 7 'OPTIONS': { 8 'context_processors': [ 9 'django.template.context_processors.debug', 10 'django.template.context_processors.request', 11 'django.contrib.auth.context_processors.auth', 12 'django.contrib.messages.context_processors.messages', 13 ], 14 }, 15 }, 16 ] 17 18 19 20 21 STATICFILES_DIRS = ( 22 os.path.join(BASE_DIR,'static'), 23 )
1 MIDDLEWARE = [ 2 'django.middleware.security.SecurityMiddleware', 3 'django.contrib.sessions.middleware.SessionMiddleware', 4 'django.middleware.common.CommonMiddleware', 5 #'django.middleware.csrf.CsrfViewMiddleware', 注释掉 6 'django.contrib.auth.middleware.AuthenticationMiddleware', 7 'django.contrib.messages.middleware.MessageMiddleware', 8 'django.middleware.clickjacking.XFrameOptionsMiddleware', 9 ]
2.templates/login.html
1 2 "UTF-8"> 326 27Title 4 "stylesheet" href="/static/commons.css"> 5 12 13 14
3.cmdb/views.py
1 def login(request): 2 3 error_msg = "" 4 if request.method == "POST": 5 user = request.POST.get('user',None) 6 pwd = request.POST.get('pwd',None) 7 if user == "root" and pwd == "123": 8 return redirect('http://www.baidu.com') 9 else: 10 error_msg = "用户名或密码错误" 11 12 return render(request,'login.html', {'error_msg': error_msg})
1.urls.py
1 urlpatterns = [ 2 url(r'home',views.home),
2.templates/home.html
1 2 "en"> 3 4 "UTF-8"> 5Title 6 7 "margin: 0"> 8"height: 48px;background-color: #dddddd">910 16171828 2919 {% for row in user_list %} 20
2721 25 {% endfor %} 26{{ row.username }} 22{{ row.gender }} 23{{ row.email }} 24
3.cmdb/views.py
1 USER_LIST = [ 2 {'username': 'zhang','email': 'zhang@163.com','gender': 'man'}, 3 {'username': 'li','email': 'li@163.com','gender': 'man'}, 4 {'username': 'wang','email': 'wang@163.com','gender': 'man'} 5 ] 6 def home(request): 7 if request.method == "POST": 8 9 u = request.POST.get('username') 10 e = request.POST.get('email') 11 g = request.POST.get('gender') 12 temp = {'username': u, 'email': e,'gender': g} 13 USER_LIST.append(temp) 14 return render(request,'home.html',{'user_list': USER_LIST})
django-admin startproject 工程名
2.创建APP
cd 工程名 python manage.py startapp cmdb
3.静态文件
project.setting.py STATICILES_DIRS = ( os.path.join(BASE_DIR,""static), )
4.模板路径
DIRS ==> [os.path.join(BASE_DIR,'templates'),]
5.settings中
url.py "login" --> 函数名
app下views.py
def func(request):
#request.method GET / POST
#http://127.0.0.1:8009/home?nid=123&name=alex
#request.POST.get('',None)
#return HttpResponse(“字符串”)
#return render(request,"HTML模板的路径")
#return redirect('/只能填URL')
特殊的模板语言
--- {{变量名}}
def func(request):
return render(request, "index.html",{'current_user':"alex"})
index.html
{{current_user}}
===>最后生成字符串
alex
for循环
def func(request):
return render(request,"index.html",{'current_user':'alex','user_list':['alex','eric']})
index.html
{{current_user}}
-
{% for row in user_list %}
- {{ row }} {% endfor %}
索引
def func(request):
return render(request,"index.html",{
'current_user':'alex',
'user_list':['alex','eric'],
'user_list':{'k1':'v1','k2':'v2'}
})
index.html
{{current_user}}
{{ user_list.1 }}
{{ user_dict.k1 }}
{{ user_dict.k2 }}
def func(request):
return render(request,"index.html",{
'current_user':'alex',
'user_list':['alex','eric'],
'user_list':{'k1':'v1','k2':'v2'}
})
index.html
{{current_user}}
{{ user_list.1 }}
{{ user_dict.k1 }}
{{ user_dict.k2 }}
{% if age %}
有年龄
{% if age > 16 %}
老男人
{% else %}
小鲜肉
{% else %}
无年零
{% endif %}
6.Django请求生命周期
-> URL对应关系(匹配) -> 试图函数 -> 返回用户字符串 -> URL对应关系(匹配) -> 试图函数 -> 打开一个HTML文件,读取内容