首页 > 代码库 > django
django
一、路由系统
1.简单的配置
├─app01
│ └─urls.py #子目录路由
│ └─views.py # 处理用户请求
│
├─s13day18django
│ ├─setting.py
│ └─urls.py #路由
└─template
├─index.html
└─detail.html # 处理用户请求
url.py
from app01 import viewsurlpatterns = [ url(r‘^admin/‘, admin.site.urls), url(r‘^index/‘, views.f1), url(r‘^login/‘, views.login),]
views.py
from django.shortcuts import renderfrom django.shortcuts import HttpResponsedef f1(request): return HttpResponse(‘OK‘)def login(request): return HttpResponse(‘OK‘)
显示效果:
2.url 数字传参
url.py
from app01 import viewsurlpatterns = [ url(r‘^detail/(\d+)/‘, views.detail), url(r‘^detail2/(\d+)/(\d+)/‘, views.detail2), url(r‘^detail3/(?P<p1>\d+)/(?P<x2>\d+)/‘, views.detail3),]
views.py
def detail(request, nid): print(nid) return HttpResponse(‘OK‘)# detail2(1,2)def detail2(request, xid, nnid): print(xid,nnid) return HttpResponse(‘OK‘)# detail3(x2=234,p1=123)def detail3(request, p1, x2): print(p1,x2) return HttpResponse(‘OK‘)
显示效果:
1)
2)
3)
3.url数字传参,分页运用
─s13day18django
└─urls.py
from app01 import viewsfrom django.conf.urls import url, include #include子目录urlpatterns = [ url(r‘^index/(\d+)/‘, views.index), url(r‘^web/‘, include(‘app01.urls‘)), ]
views.py
from django.shortcuts import renderUSER_LIST = []for item in range(94): temp = {"id": item, ‘username‘:‘alex‘+str(item), ‘email‘: ‘email‘ + str(item)} USER_LIST.append(temp)def index(request, page): print(page) # 1,0-9 # 2,10-19 # 3,20-29 page = int(page) start = (page - 1) * 10 end = page * 10 user_list = USER_LIST[start:end] # return HttpResponse(‘OK‘) return render(request, ‘index.html‘, {‘user_list‘: user_list})def detail(request, nid): nid = int(nid) current_detail_dict = USER_LIST[nid] # current_detail_dict return render(request, ‘detail.html‘, {‘current_detail_dict‘: current_detail_dict})
─app01 #子目录
└─urls.py
from app01 import viewsurlpatterns = [ url(r‘^detail/(\d+)/‘, views.detail),]
index.html
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title></title></head><body> <table> <tr> <td>ID</td> <td>用户名</td> <td>详细</td> </tr> {% for item in user_list %} <tr> <td>{{ item.id }}</td> <td>{{ item.username }}</td> <td><a href="/web/detail/{{ item.id }}/">查看详细</a></td> </tr> {% endfor %} </table></body></html>
detail.html
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title></title></head><body> <ul> <li> {{ current_detail_dict.id }} </li> <li> {{ current_detail_dict.username }} </li> <li> {{ current_detail_dict.email }} </li> </ul></body></html>
setting.py
INSTALLED_APPS = [ ‘django.contrib.admin‘, ‘django.contrib.auth‘, ‘django.contrib.contenttypes‘, ‘django.contrib.sessions‘, ‘django.contrib.messages‘, ‘django.contrib.staticfiles‘, ‘app01‘, #添加]
4.各种数据类型,页面显示 以及自定义属性
├─app01
│ ├─urls.py #子目录路由
│ ├─views.py # 处理用户请求
│ └─templatetags
│ └─ xx.py
├─s13day18django
│ └─urls.py #路由
└─template
└─detail.html # 处理用户请求
─s13day18django
└─urls.py
from app01 import viewsfrom django.conf.urls import url, include #include子目录urlpatterns = [ url(r‘^web/‘, include(‘app01.urls‘)), ]
├─app01
└─urls.py
from django.conf.urls import urlfrom app01 import viewsurlpatterns = [ url(r‘^template‘, views.template),]
views.py
def template(request): return render(request, ‘template.html‘, {‘k1‘:‘VVVV‘,‘k2‘:[11,22,33],‘k3‘: {‘nid‘:12,‘name‘: ‘alex‘}})
xx.py
@register.filterdef f1(value, arg): return value + "666" + arg@register.simple_tagdef f2(s1,s2,s3, s4): return s1 + s2 + s3 + s4@register.filterdef f3(value): if value =http://www.mamicode.com/= ‘VVV‘: return True return False
template.html
{% load xx %} <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title></title></head><body> {{ k1 }} {{ k2.1 }} {{ k3.name }} {% for item in k2 %} <p>{{ item }},{{ forloop.counter }},{{ forloop.counter0 }},{{ forloop.first }},{{ forloop.last }},{{ forloop.revcounter }}</p> {% endfor %} {% if k1 == ‘v1‘ %} <h1>V1</h1> {% elif k1 == ‘v2‘ %} <h1>V2</h1> {% else %} <h1>7777</h1> {% endif %} {{ k1 }} {{ k1|lower }} #内置方法 {{ k1|f1:"alex,123" }} #自定义方法一 对应 xx.py @register.filter {% f2 1 2 3 4 %} #自定义方法二 对应xx.py @register.simple_tag 不能用于if判断 {% if k1|f3 %} <h1>True</h1> {% else %} <h1>False</h1> {% endif %}</body></html>
显示效果:
5.模板-
├─app01
│ ├─urls.py #子目录路由
│ └─views.py # 处理用户请求
├─s13day18django
│ └─urls.py #路由
└─template
├─assets.html
├─userinfo.html
├─layout.html # 处理用户请求
└─son.html
─s13day18django
└─urls.py
from app01 import viewsfrom django.conf.urls import url, include #include子目录urlpatterns = [ url(r‘^web/‘, include(‘app01.urls‘)), ]
├─app01
└─urls.py
from django.conf.urls import urlfrom app01 import viewsurlpatterns = [ url(r‘^assets‘, views.assets), url(r‘^userinfo‘, views.userinfo),]
views.py
def assets(request): assets_list = [] for i in range(10): temp = {‘hostname‘: ‘h1‘+str(i), ‘port‘: 80} assets_list.append(temp) return render(request, ‘assets.html‘, {‘assets_list‘: assets_list})def userinfo(request): user_list = [] for i in range(10): temp = {‘username‘: ‘h1‘+str(i), ‘salary‘: 80} user_list.append(temp) return render(request, ‘userinfo.html‘, {"user_list": user_list})
ayout.html
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title></title> <link rel="stylesheet" href=http://www.mamicode.com/"..." /> <style> .pg-header{ height: 48px; background-color: cadetblue; } .pg-body{ min-height: 500px; } .pg-body .body-menu{ width: 20%; float: left; } .pg-body .body-content{ width: 80%; float: left; } .pg-footer{ height: 100px; background-color: brown; } .active{ background-color: aquamarine; color: white; } </style> {% block css %}{% endblock %}</head><body> <div class="pg-header"> 后台系统V1 </div> <div class="pg-body"> <div class="body-menu"> <ul> <li><a id="userinfo" href=http://www.mamicode.com/"/web/userinfo">用户管理</a></li> <li><a id="assets" href=http://www.mamicode.com/"/web/assets">资产管理</a></li> </ul> </div> <div class="body-content"> {% block body %}{% endblock %} </div> </div> <div class="pg-footer"></div> <script src=http://www.mamicode.com/"xxx"></script> {% block js %}{% endblock %}</body></html>
assets.html
{% extends ‘layout.html‘ %}{% block body %} <table> {% for item in assets_list %} <tr> <td>{{ item.hostname }}</td> <td>{{ item.port }}</td> </tr> {% endfor %} </table> {% include ‘son.html‘ %} {% include ‘son.html‘ %} {% include ‘son.html‘ %} {% include ‘son.html‘ %} {% include ‘son.html‘ %} {% include ‘son.html‘ %} <h3></h3>{% endblock %}
userinfo.html
{% extends ‘layout.html‘ %}{% block css %} <style></style>{% endblock %}{% block body %} <ul> {% for item in user_list %} <li>{{ item.username }},{{ item.salary }}</li> {% endfor %} </ul>{% endblock %}{% block js %} <script> document.getElementById(‘userinfo‘).className = ‘active‘; </script>{% endblock %}
son.html
<form> <input /> <input /> <input /> <input /></form>
显示效果
5.登陆认证
├─app01
│ ├─urls.py #子目录路由
│ └─views.py # 处理用户请求
├─s13day18django
│ └─urls.py #路由
└─template
└─ajax_demo.html
─s13day18django
└─urls.py
from app01 import viewsfrom django.conf.urls import url, include #include子目录urlpatterns = [ url(r‘^web/‘, include(‘app01.urls‘)), ]
├─app01
└─urls.py
from django.conf.urls import urlfrom app01 import viewsurlpatterns = [ url(r‘^ajax_demo‘, views.ajax_demo),]
views.py
from django.shortcuts import renderfrom django.shortcuts import HttpResponseimport jsondef ajax_demo(request): if request.method == ‘POST‘: ret = {‘status‘: False, ‘message‘: ‘‘} user = request.POST.get(‘user‘,None) pwd = request.POST.get(‘pwd‘,None) if user == ‘111‘ and pwd == ‘222‘: ret[‘status‘] = True return HttpResponse(json.dumps(ret)) else: ret[‘message‘] = "用户名或密码错误" return HttpResponse(json.dumps(ret)) return render(request, ‘ajax_demo.html‘)
ajax_demo.html
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title></title></head><body> <div> <p>用户名:<input type="text" id="username" /></p> </div> <div> <p>用户名:<input type="password" id="pwd" /></p> </div> <input type="button" value=http://www.mamicode.com/"提交" onclick="SubmitForm();" /> <script src=http://www.mamicode.com/"/static/jquery-1.8.2.min.js"></script> <script> function SubmitForm(){ $.ajax({ url: ‘/web/ajax_demo/‘, type: ‘POST‘, data: {‘user‘: $(‘#username‘).val(), ‘pwd‘: $(‘#pwd‘).val()}, dataType: ‘json‘, success: function (data) { // data = http://www.mamicode.com/字符串 {status:xx,message:‘‘} // data对象 //var data_dict = JSON.parse(data); if(data.status){ location.href = "http://www.baidu.com"; }else{ alert(data.message); } } }) } </script></body></html>
显示效果:登陆后跳转到百度
django