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

复制代码
相关推荐
tyqtyq221 小时前
HarmonyOS AI 应用开发实战:考研择校分析系统
人工智能·学习·考研·华为·生活·harmonyos
光影6271 小时前
MySQL基础入门
数据库·笔记·sql·学习·mysql·学习方法
海兰1 小时前
【实用程序】网页公开信息智能采集系统详细设计指南
人工智能·学习·自动化·采集信息
MartinYeung51 小时前
[论文学习]AgentDAM:自主Web Agent的隐私泄露评估
学习
光影6272 小时前
MySQL 进阶篇 —— 事务 / 外键 / 索引 / 高级查询
数据库·笔记·sql·学习·mysql
醉城夜风~3 小时前
MyBatis 基础CRUD全套实战学习笔记
笔记·学习·mybatis
blues92573 小时前
2026商超采购竹笋怎么选:从包装信息、货架周转到售后协同做判断
人工智能·科技·学习
Flandern11114 小时前
从“能跑”到“可运营”:Agent Harness 工程化建设指南
学习·agent·claudecode·harness
hj2862514 小时前
Shell 脚本完整学习笔记
chrome·笔记·学习
m0_377108144 小时前
cmake学习
学习