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

复制代码
相关推荐
speop12 小时前
【datawhale组队学习】TASK01|课程导论:站在认知范式的临界点
人工智能·学习
roman_日积跬步-终至千里13 小时前
【人工智能原理(1)】要点总结:从搜索、学习到推理的智能之路
人工智能·学习
我不会写代码njdjnssj13 小时前
SSM框架学习
学习
阿蒙Amon13 小时前
JavaScript学习笔记:14.类型数组
javascript·笔记·学习
代码游侠14 小时前
学习笔记——写时复制(Copy-on-Write)
linux·网络·笔记·学习·写时复制
阿蒙Amon14 小时前
JavaScript学习笔记:3.控制流与错误处理
javascript·笔记·学习
HalvmånEver14 小时前
Linux:基础IO(终)
linux·运维·c++·学习·缓冲区·libc
昵称只无法修改14 小时前
计算机底层原理
学习
charlie11451419114 小时前
在上位机上熟悉FreeRTOS API
笔记·学习·嵌入式·c·freertos·工程
EveryPossible14 小时前
状态丢失问题
学习