Django页面跳转设计梗概

[复制链接]
查看: 610   回复: 0

250

主题

250

帖子

1389

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1389
2024-2-16 16:38:02   显示全部楼层   阅读模式  
通用封面.jpg


1.首先路由方面要设计好了

app目录下子路urls.py
from collections import UserList

from django.urls import path, include
from APP.views import *

urlpatterns = [
    # 首页
    # path('list/', APP.views.index)
    path('index/', index, name='index'),

    path('userList/', userList, name='userList')
]
做了两个页面 用户首页和 用户列表页面

2.然后是渲染函数view视图对应两个
from django.shortcuts import render


# Create your views here.

def index(request):
    pass
    return render(request, 'userIndex.html')


def userList(request):
    return render(request, 'userList.html')

3.接下来就是django中html特有的一些语法书写

1.png

第三种跳转方案 具体要看跟路由有没有配置路由命名空间 就一个app就可以省略
# 2. 使用子路由 namespace 命名空间

urlpatterns += [
    # path('user/', include('APP.urls', 'APP'), namespace='user')
    path('user/', include(('APP.urls', 'APP'), namespace='Uapp')),
]
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户首页</title>
</head>
<body>
<h1>用户首页</h1>
<hr>
{# url路径跳转 #}
<a href="/user/userList/">a标签点得方式 进入用户列表页面</a>
<hr>
{# 反向解析 #}
<a href="{% url 'Uapp:userList' %}">反向解析的方式 进入用户列表页面</a>
{##}
{# userList就是我们path路径里面name名字 #}
<hr>
{# 反向解析 带命名空间 ,多app时候跳转命名空间可以防止多个app打架#}
<a href="{% url 'Uapp:userList' %}">反向解析的方式加上namespace命名空间得跳转路由 进入用户列表页面</a>

</body>
</html>

回复

使用道具 举报

您需要登录后才可以回帖   登录 立即注册

高级模式

南通谢凡软件科技有限公司