第10章节-Python3.5-Django路由分发

通过include 来实现Django路由分发(通过一级路由和二级路由)

  • $效果图:

image.png
image.png
  • 一级路由:


    image.png
  • 修改urls.py:

from django.conf.urls import url,include
from django.contrib import admin
# from app01 import views as V1
# from app02 import views as V2
# from app01 import views

urlpatterns = [
    url(r'^cmdb/', include("app01.urls")),
    url(r'^monitor/', include("app02.urls")),
]
  • 二级路由:
image.png
  • 修改app01\urls.py
from django.conf.urls import url,include
from django.contrib import admin
from app01 import views


urlpatterns = [
    url(r'^login/', views.login),
]


  • 二级路由:


    image.png
  • 修改app02\urls.py

from django.conf.urls import url,include
from django.contrib import admin
from app02 import views


urlpatterns = [
    url(r'^login/', views.login),
]


image.png
  • 修改views.py
from django.shortcuts import render,HttpResponse

# Create your views here.


def login(request):
    return HttpResponse('APP02,login')

  • index.html:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form action="{% url 'indexx' nid=1 uid=3 %}" method="post">
        <p><input type="text" name="user" placeholder="用户名"></p>
        <p><input type="text" name="email" placeholder="邮箱"></p>
        <input type="submit" value="提交">
    </form>

    <ul>
        {% for k,row in user_dict.items %}
        <!--target="_blank"表示在新页面打开-->
        <li><a target="_blank" href="/detail-{{ k }}.html">{{ row.name }}</a></li>
        {% endfor %}
    </ul>

</body>
</html>
  • login.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <!--# 如果要上传文件表单要加上enctype="multipart/form-data"-->
    <form action="/login/" method="post" enctype="multipart/form-data">
        <p>
            <input type="text" name="user" placeholder="用户名">
        </p>
        <p>
            <input type="password" name="pwd" placeholder="密码">
        </p>
        <p>
            男: <input type="radio" name="gender" value="1">
            女: <input type="radio" name="gender" value="2">
            卡米: <input type="radio" name="gender" value="3">
        </p>
        <p>
            男: <input type="checkbox" name="favor" value="11">
            女: <input type="checkbox" name="favor" value="22">
            卡米: <input type="checkbox" name="favor" value="33">
        </p>
        <p>
            <select name="city" id="">
                <option value="sh">上海</option>
                <option value="bj">北京</option>
                <option value="tj">天津</option>
            </select>
        </p>
        <p>
            <!--上传文件的标签-->
            <input type="file" name="fff">
        </p>
        <input type="submit" value="提交">

    </form>

</body>
</html>
上一篇:win10下Welcome.java:3: 错误: 编码GBK的不可映射字符 System.out.println("Hello Java!


下一篇:django创建项目案例1详细展示续05