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

复制代码
相关推荐
db_murphy1 小时前
学习篇 | 英方i2Active和i2Stream工具了解
学习
强子感冒了2 小时前
Java学习笔记:String、StringBuilder与StringBuffer
java·开发语言·笔记·学习
BullSmall2 小时前
Doris的备份及恢复方案
学习
小李子不吃李子2 小时前
人工智能与创新第二章练习题
人工智能·学习
NULL指向我3 小时前
STM32F407VET6学习笔记14:Bootloader程序笔记
笔记·stm32·学习
别了,李亚普诺夫3 小时前
定时器与输出比较-学习笔记
笔记·stm32·学习
于越海4 小时前
学习小项目:用 Python 自动统计编程课绩点(5.0 制|百分制直算|重修取最高)
开发语言·笔记·python·学习·学习方法
confiself4 小时前
UI-Ins技术报告学习
学习
ndjnddjxn5 小时前
Python学习-1
学习
一 乐6 小时前
健康管理|基于springboot + vue健康管理系统(源码+数据库+文档)
java·前端·数据库·vue.js·spring boot·后端·学习