Djang学习- URL反转

代码中url书写规范:

url反向解析

urls:

python 复制代码
path('test/url', views.test_url),
    path('test_result/<int:age>', views.test_result, name="rl")

views:

python 复制代码
def test_url(request):
    return render(request, 'test_url.html')


def test_result(request,age):
    return HttpResponse("--test----result---")

templates:

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<a href="http://127.0.0.1:8000/test_result">绝对地址</a>
<a href="/test_result">带'/'的相对地址</a>
<a href="test_result">不带'/'的相对地址</a>
<a href="{% url 'rl' '100'%}">反向解析跳转</a>
</body>
</html>
python 复制代码
def test_result(request,age):
    # 302 重定向
    from django.urls import reverse
    from django.http import HttpResponseRedirect
    from django.shortcuts import redirect
    url = reverse('base',args=[10])
    return redirect(url)

可以使用HttpResponseRedirect和redirect进行重定向,不能直接使用HttpResponse,HttpResponse会直接输出你要重定向的url,而不是去重定向

复制代码
相关推荐
math_hongfan7 分钟前
鸿蒙离线数据缓存高级架构:弱网预加载/离线数据优先级/同步冲突解决/上线后数据合并策略
学习·缓存·华为·架构·harmonyos·鸿蒙
划水的code搬运工小李17 分钟前
Simulink学习-自定义Storage Class配置
学习
一尘之中1 小时前
量子计算机能“参禅”吗?从叠加态到不二法门的技术哲学追问
学习·ai写作·量子计算
什么都干的派森2 小时前
高速PCB设计学习记录(更新中)
学习·高速pcb设计
lifallen2 小时前
Emdash 拆解:多 Agent 并行开发桌面端的实现思路,兼谈 ACP 与 A2A
人工智能·学习·ai·ai编程
迪丽热爱3 小时前
每日英语-7
学习
LccKyI4 小时前
C#学习day04
经验分享·笔记·学习·c#
math_hongfan4 小时前
鸿蒙企业级数据存储高级架构:从读写分离到冷热数据分层/归档策略/数据生命周期管理最佳实践
人工智能·学习·华为·架构·harmonyos·鸿蒙
MartinYeung54 小时前
[论文学习]LLM智能体无法保密:多智能体系统中的隐私评估
人工智能·学习
、达西先生4 小时前
GMR IK数学原理解读
笔记·学习·il·遥操作·模仿学习