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

复制代码
相关推荐
郝学胜-神的一滴11 小时前
Effective Modern C++ 条款40:深入理解 Atomic 与 Volatile 的多线程语义
开发语言·c++·学习·算法·设计模式·架构
追光少年332211 小时前
React学习:ES6
学习·react.js·es6
宇木灵11 小时前
考研数学-高中数学回顾函数的微分day8(完结)
笔记·学习·考研·数学·函数·导数·微分
今儿敲了吗11 小时前
28| A-B数对
数据结构·c++·笔记·学习·算法
The_Uniform_C@t211 小时前
AWD | ATT&CK实战系列--蓝队防御(一)
网络·学习·网络安全
今儿敲了吗12 小时前
27| 魔法封印
数据结构·c++·笔记·学习·算法
2501_9181269112 小时前
stm32能做次声波检测器吗?
c语言·stm32·单片机·嵌入式硬件·学习
好好学习天天向上~~12 小时前
13_Linux_学习总结_进程终止
linux·学习
今儿敲了吗12 小时前
30| 木材加工
数据结构·c++·笔记·学习·算法
锅包一切12 小时前
PART7 队列
c++·学习·算法·leetcode·力扣·刷题·队列