第八章(1)


目录

  1. js正册表达式
  2. 提交事件顺序
  3. web模板
  4. Django
  5. Django内容整理
  6. 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
		submit
		
		
$(':submit').click(function(){ $(':text,:password').each(function(){ retuen false; }) return false; })

3.web模板

Ajax操作
http://www.jeasyui.com/

jqyeryui
http://jqueryui.com/

bootstrap
http://www.bootcss.com/
https://v3.bootcss.com/

bootstrap 模板
http://www.cssmoban.com/tags.asp?page=3&n=Bootstrap

bootstrap(响应式)

@media

   


    

!important(强制样式生效)


10 
11 
12     "/login" method="post">
13         

14 15 "username" type="text"/> 16

17

18 19 "password" type="text" /> 20 "submit" value="提交" /> 21

22 23 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 )

django验证用户密码是否正确

1.settings.py

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">
 3     Title
 4     "stylesheet" href="/static/commons.css">
 5     
12 
13 
14     
"/login" method="post"> 15

16 17 "username" name="user" type="text"/> 18

19

20 21 "password" name="pwd" type="password" /> 22 "submit" value="提交" /> 23 "color: red;">{{ error_msg }} #### 24

25
26 27

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})

input添加示例

1.urls.py

1 urlpatterns = [
2     url(r'home',views.home),

2.templates/home.html

 1 
 2 "en">
 3 
 4     "UTF-8">
 5     Title
 6 
 7 "margin: 0">
 8     
"height: 48px;background-color: #dddddd">
9
10
"/home" method="post"> 11 "text" name="username" placeholder="用户名"/> 12 "text" name="email" placeholder="邮箱"/> 13 "text" name="gender" placeholder="性别"/> 14 "submit" value="添加"/> 15
16
17
18 19 {% for row in user_list %} 202122232425 {% endfor %} 26
{{ row.username }} {{ row.gender }} {{ row.email }}
27
28 29

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})

内容整理

1.创建Django工程

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" --> 函数名

7.定义视图函数

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')

8.模板渲染

特殊的模板语言

--- {{变量名}}

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文件,读取内容