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

复制代码
相关推荐
代码AC不AC41 分钟前
【C++】异常
c++·学习·异常
Cristiano777.1 小时前
周学习记录
学习
py有趣3 小时前
LeetCode算法学习之鸡蛋掉落
学习·算法·leetcode
小马爱打代码3 小时前
RabbitMQ:系统学习笔记
笔记·学习·rabbitmq
YJlio3 小时前
进程和诊断工具速查手册(8.13):VMMap / DebugView / LiveKd / Handle / ListDLLs 一页式现场排障清单
数据库·笔记·学习
青衫码上行5 小时前
【Java Web学习 | 第12篇】JavaScript(6)DOM
java·开发语言·前端·javascript·学习
YangYang9YangYan5 小时前
中专生学历提升与职业发展指南
大数据·人工智能·学习·数据分析
深蓝海拓5 小时前
YOLO v11的学习记录(五) 使用自定义数据从头训练一个实例分割的模型
学习·yolo
Gary Studio5 小时前
鋰電池充電芯片學習
学习
菜鸟‍6 小时前
【前端学习】React学习【万字总结】
前端·学习·react.js