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

复制代码
相关推荐
pumpkin845141 小时前
Go 学习全景引子:理解设计理念与工程思路
python·学习·golang
week_泽1 小时前
第5课:短期记忆与长期记忆原理 - 学习笔记_5
java·笔记·学习·ai agent
星期五不见面2 小时前
机器人学习!(二)ROS-基于Gazebo项目(2)2026/01/12
学习·机器人
Freshman小白2 小时前
《智能制造系统》网课答案
学习·答案·网课答案
副露のmagic2 小时前
更弱智的算法学习 day34
python·学习
写点什么呢3 小时前
AD21安装激活
学习
wdfk_prog4 小时前
[Linux]学习笔记系列 -- bits
linux·笔记·学习
求梦8204 小时前
JVM学习
jvm·学习
星火开发设计4 小时前
C++ multiset 全面解析与实战指南
开发语言·数据结构·c++·学习·set·知识
am心4 小时前
学习笔记-菜品接口-菜品分页查询
笔记·学习