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

复制代码
相关推荐
我命由我123451 小时前
Photoshop - Photoshop 工具栏(67)修补工具
学习·ui·职场和发展·求职招聘·职场发展·学习方法·photoshop
DeepModel2 小时前
第15章 多模态学习
深度学习·学习·机器学习
不吃橘子的橘猫2 小时前
《集成电路设计》复习资料2(设计基础与方法)
学习·算法·fpga开发·集成电路·仿真·半导体
不吃橘子的橘猫4 小时前
《集成电路设计》复习资料4(Verilog HDL概述)
学习·算法·fpga开发·集成电路·仿真·半导体
宇木灵4 小时前
C语言基础-五、数组
c语言·开发语言·学习·算法
楼田莉子4 小时前
Linux学习:线程的同步与互斥
linux·运维·c++·学习
春和景明3607 小时前
复习数据库
学习
wostcdk7 小时前
数论学习1
数据结构·学习·算法
babe小鑫8 小时前
大专工业大数据应用专业学习数据分析的价值分析
大数据·学习·数据分析
梦雨羊8 小时前
LitGPT
学习