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

复制代码
相关推荐
时间会给答案scidag6 分钟前
Spring AI Alibaba 学习day01
人工智能·学习·spring
风送雨1 小时前
FastAPI 学习教程 · 第4部分
学习·fastapi
binbinaishijie881 小时前
ANSYS Workbench完全入门教程|0基础学习路线图
学习·其他
半夏知半秋2 小时前
kcp学习-通用的kcp lua绑定
服务器·开发语言·笔记·后端·学习
曾浩轩2 小时前
图灵完备Turing Complete 2
学习·图灵完备
A小码哥3 小时前
跟着AI学习谷歌最新的通用商业协议(UCP)实操步骤
人工智能·学习
科技林总3 小时前
【系统分析师】4.2 网络体系结构
学习
且去填词3 小时前
深入理解 GMP 模型:Go 高并发的基石
开发语言·后端·学习·算法·面试·golang·go
Yuzhiyuxia3 小时前
【设计模式】设计模式学习总结
学习·设计模式
小裕哥略帅4 小时前
PMP知识--五大过程组
笔记·学习