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,而不是去重定向

复制代码
相关推荐
_Kayo_1 小时前
VUE2 学习笔记5 动态绑定class、条件渲染、列表过滤与排序
笔记·学习
waveee1231 小时前
学习嵌入式的第三十四天-数据结构-(2025.7.29)数据库
数据结构·数据库·学习
xiaoxiaoxiaolll3 小时前
Adv. Sci. 前沿:非零高斯曲率3D结构可逆转换!液晶弹性体多级形变新策略
学习
xiaoli23274 小时前
课题学习笔记3——SBERT
笔记·学习·nlp·bert
缘友一世5 小时前
Agent常用搜索引擎Tavily使用学习
学习·搜索引擎·agent
超浪的晨6 小时前
JavaWeb 入门:JavaScript 基础与实战详解(Java 开发者视角)
java·开发语言·前端·javascript·后端·学习·个人开发
Magnetic_h6 小时前
【iOS】类和分类的加载过程
笔记·学习·ios·objective-c·xcode
努力的小帅7 小时前
C++_红黑树树
开发语言·数据结构·c++·学习·算法·红黑树
hweiyu007 小时前
学习Scala语言的最佳实践有哪些?
开发语言·学习·scala
停走的风7 小时前
Yolo底层原理学习--(第二篇)
深度学习·学习·yolo