Python 【第十章】 Django路由

路由可以简单理解就是

URL -> 函数名

例如:

/login1/ -> 函数名

urls.py文件中

urlpatterns = [
    # url(r'^admin/', admin.site.urls),
    url(r'^index/', views.index),
    url(r'^index1/', views.index),
]

#从下向下匹配,如果上面URL匹配成功下面就不进行匹配处理

如果要处理很多URL请求时,第一个处理方法,是每个URL写一个对应函数。这样效率不高,优处理方法是通过正则表达式处理:

路由关系中动态参数:

例子1:传递一个参数

urls.py文件中

urlpatterns = [

    url(r'^login/(d+)/', views.login),
]

views.py文件中接收时注意,传入随URL还有一个数字,所以要有两个参数。

def login(request,nid):
    print(nid)  #nid就是传入数字参数
    request HttpResponse('OK')

例子2:传递多个参数

urls.py文件中

urlpatterns = [

    url(r'^login/(d+)(d+)/', views.login),
]

views.py文件中接收时注意,传入随URL还有一个数字,所以要有多个参数。

def login(request,nid1,nid2):
    print(nid1,nid2)  #nid就是传入数字参数
    request HttpResponse('OK')

例子3:根据名字传递参数

urls.py文件中

urlpatterns = [

    url(r'^login/(?P<p1>d+)/(?P<x2>d+)/', views.login), ]

views.py文件中接收时注意,传入随URL形参数名称要对应。

def login(request,p1,x2):
    print(p1,x2)  # 就是传入数字参数
    request HttpResponse('OK')

例子3:路由实现简单分页

views.py

USER_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):

    page = int(page)
    start = (page-1)*10
    end = page * 10
    user_list = USER_LIST[start:end]
    return render(request,'index.html',{'user_list':user_list})

def detail(request,nid):
    #详细页
    nid = int(nid)
    current_detail_dict = USER_LIST[nid]
    return render(request,'detail.html',{'current_detail_dict':current_detail_dict})

urls.py

from django.conf.urls import url
from django.contrib import admin
from cmdb import views

urlpatterns = [
   
    url(r'^index/(d+)', views.index),
    url(r'^detail/(d+)', views.detail),
 ]

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<h1 style="color: red">123</h1>
<h1>用户输入:</h1>
<form action="/index/" method="POST">
    <input type="text" name="user" />
    <input type="text" name="email" />
    <input type="submit" value="提交" />

</form>
<h1>数据展示:</h1>
<table border="1">
    <tr>
        <th>ID:</th>
        <th>用户名:</th>
        <th>详细信息:</th>
    </tr>
    {% for line in user_list %}
    <tr>
        <td>{{ line.id }}</td>
        <td>{{ line.username }}</td>
        <td><a href="/detail/{{ line.id }}">查看详细</a></td>
    </tr>

    {% endfor %}
</table>




{#<script src="/statics/jquery-1.8.2.min.js"></script>#}
</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>

效果:

点入查看详细进入详细页detail.html

  路由分发

在实现中遇到有多个app,每个app相互独立,在urls.py中要建立多个路由进行对应,效率不高。

改进方案,进行分级urls调置,第一级urls接收到,再转给 app内urls,进行分发

 

 第一级urls.py

"""mysite URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.10/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.conf.urls import url, include
    2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.contrib import admin
from cmdb import views
from django.conf.urls import url, include

urlpatterns = [
    url(r'^web/', include('cmdb.urls')),
]

 app内建立一个urls.py

 

  index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<h1 style="color: red">123</h1>
<h1>用户输入:</h1>
<form action="/index/" method="POST">
    <input type="text" name="user" />
    <input type="text" name="email" />
    <input type="submit" value="提交" />

</form>
<h1>数据展示:</h1>
<table border="1">
    <tr>
        <th>ID:</th>
        <th>用户名:</th>
        <th>详细信息:</th>
    </tr>
    {% for line in user_list %}
    <tr>
        <td>{{ line.id }}</td>
        <td>{{ line.username }}</td>
        <td><a href="/web/detail/{{ line.id }}">查看详细</a></td>
    </tr>

    {% endfor %}
</table>




{#<script src="/statics/jquery-1.8.2.min.js"></script>#}
</body>
</html>

效果在网页输入web/index/1 这样通过web来明确访问那个app

 

原文地址:https://www.cnblogs.com/yaabb163/p/6242529.html