首页 > 代码库 > web前端基础知识-(七)Django进阶

web前端基础知识-(七)Django进阶

通过上节课的学习,我们已经对Django有了简单的了解,现在来深入了解下~

1. 路由系统

1.1 单一路由对应

url(r‘^index$‘, views.index),

1.2 基于正则的路由

url(r‘^index/(\d*)‘, views.index),
url(r‘^manage/(?P<name>\w*)/(?P<id>\d*)‘, views.manage),
  • 找到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),
]
  • 在views.py文件创建对应方法
USER_DICT = {
    ‘1‘:{‘name‘:‘root1‘,‘email‘:‘root@live.com‘},
    ‘2‘:{‘name‘:‘root2‘,‘email‘:‘root@live.com‘},
    ‘3‘:{‘name‘:‘root3‘,‘email‘:‘root@live.com‘},
    ‘4‘:{‘name‘:‘root4‘,‘email‘:‘root@live.com‘},
}
 
def index(request):
    return render(request,"index.html",{"user_dict":USER_DICT})
 
def detail(request,nid):  # nid指定的是(\d+)里的内容
    detail_info = USER_DICT[nid]
    return render(request, "detail.html", {"detail_info": detail_info})

1.3 url分组

在url.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-(?P<nid>\d+)-(?P<uid>\d+).html/‘, views.detail),<br>   # nid=\d+ uid=\d+
]

在views.py文件创建对应方法

def detail(request,**kwargs):
    print(kwargs)          
    #{‘nid‘: ‘4‘, ‘uid‘: ‘3‘}
    nid = kwargs.get("nid")
    detail_info = USER_DICT[nid]
    return render(request, "detail.html", {"detail_info": detail_info})

1.4 为路由映射名称

from django.conf.urls import url,include
from django.contrib import admin
from cmdb import views
 
urlpatterns = [
    url(r‘^asdfasdfasdf/‘, views.index, name=‘i1‘),     #第一种方式i1
    url(r‘^yug/(\d+)/(\d+)/‘, views.index, name=‘i2‘),  #第二种方式i2
    url(r‘^buy/(?P<pid>\d+)/(?P<nid>\d+)/‘, views.index, name=‘i3‘),    #第三种方式i3
]

在templates目录下的index.html

<body>
{#第一种方法i1       路径asdfasdfasdf/#}
{#<form action="{% url "i1" %}" method="post">#}
{#第二种方法i2       路径yug/1/2/#}
{#<form action="{% url "i2" 1 2 %}" method="post">#}
{#第三种方法i3       路径buy/1/9//#}
<form action="{% url "i3" pid=1 nid=9 %}" method="post">
    <p><input  name="user" type="text" placeholder="用户名"/></p>
    <p><input  name="password" type="password" placeholder="密码"/></p>
    <p><input type="submit" value="http://www.mamicode.com/提交"/></p>
</form>
</body>

1.5 根据app对路由分类

主程序urls.py文件

from django.conf.urls import url,include
from django.contrib import admin
 
urlpatterns = [
    url(r‘^monitor/‘, include(‘monitor.urls‘)), #调整到monitor目录中的urls.py文件
]

cmdb下的url.py文件

from django.conf.urls import url
from django.contrib import admin
from monitor import views
#
urlpatterns = [
    url(r‘^login‘, views.login),
]

1.6 获取当前URL

view.py中配置

def index(request):
    print(request.path_info)    #获取客户端当前的访问链接
    # / index
    return render(request,"index.html",{"user_dict":USER_DICT})

在templates目录下的index.html文件

<form action="{{ request.path_info }}" method="post">
    <p><input  name="user" type="text" placeholder="用户名"/></p>
    <p><input  name="password" type="password" placeholder="密码"/></p>
    <p><input type="submit" value="http://www.mamicode.com/提交"/></p>
</form>

2. 视图

3. ORM操作

web前端基础知识-(七)Django进阶