Django 解析路由参数

编写带url参数的路由,4种类型参数

app1.url.py

复制代码
from django.urls import path
from . import views

urlpatterns = [
    path('index', views.index, name='index'),
    path('test', views.test, name='test'),
    path('test_int/<int:id>/', views.test_int, name='test_int'),
    path('test_str/<str:strdata>/', views.test_str, name='test_str'),
    path('test_slug/<slug:slugdata>/', views.test_slug, name='test_slug'),
    path('book_uuid/<uuid:bookuuid>/', views.book_uuid, name='book_uuid'),
]

app1.views.py

复制代码
from django.http import HttpResponse
from django.shortcuts import render
def index(request):
    return HttpResponse("app1 的index")

def test(request):
    return render(request, '1/index.html', {})

def test_int(request, id):
    # 你可以在这里使用 'id',比如从数据库获取对应的数据
    # ...
    response = HttpResponse(f"test_int路由访问参数:{id}")
    response['Content-Type'] = "text/html; charset=utf-8"
    return response

def test_str(request, strdata):
    # 你可以在这里使用 'id',比如从数据库获取对应的数据
    # ...
    response = HttpResponse(f"test_str路由访问参数:{strdata}")
    response['Content-Type'] = "text/html; charset=utf-8"
    return response

def test_slug(request, slugdata):
    # 你可以在这里使用 'id',比如从数据库获取对应的数据
    # ...
    response = HttpResponse(f"test_slug路由访问参数:{slugdata}")
    response['Content-Type'] = "text/html; charset=utf-8"
    return response

from uuid import UUID

def book_uuid(request, bookuuid: UUID):
    # 在这里,'bookuuid' 是一个UUID 对象。
    return HttpResponse(f"Book's UUID is: {str(bookuuid)}")
相关推荐
源码之家14 分钟前
计算机毕业设计:Python棉花产业数据可视化与预测系统 Django框架 ARIMA算法 数据分析 可视化 爬虫 大数据 大模型(建议收藏)✅
人工智能·python·算法·信息可视化·数据挖掘·django·课程设计
源码之家33 分钟前
计算机毕业设计:Python农产品智能推荐与可视化分析系统 Flask框架 矩阵分解 数据分析 可视化 协同过滤推荐算法 深度学习(建议收藏)✅
python·矩阵·数据挖掘·数据分析·django·flask·课程设计
Betelgeuse7634 分钟前
打通 Django 认证:原生 Auth 组件实战与 API 改造
后端·python·django
青花瓷38 分钟前
PyCharm中创建Django基础网站
ide·pycharm·django
天选之子1231 小时前
Django模板(二)
数据库·django·sqlite
小熊Coding2 小时前
Python二手房数据可视化分析+预测+推荐
开发语言·python·信息可视化·django·计算机毕业设计·二手房数据分析·二手房数据可视化分析
码界筑梦坊18 小时前
356-基于Python的网易新闻数据分析系统
python·mysql·信息可视化·数据分析·django·vue·毕业设计
源码之家21 小时前
计算机毕业设计:Python农产品销售数据可视化分析系统 Django框架 数据分析 可视化 大数据 大模型 机器学习(建议收藏)✅
python·信息可视化·数据分析·django·flask·课程设计
YJlio1 天前
2026年4月18日60秒读懂世界:从神舟二十号出舱到L2新国标公示,今天最值得关注的6个信号
windows·python·django·计算机外设·电脑·outlook·eixv3
源码之家1 天前
计算机毕业设计:Python降水量分析与预警平台 Flask框架 数据分析 可视化 大数据 AI 大模型 爬虫 数据大屏(建议收藏)✅
人工智能·python·信息可视化·数据分析·django·flask·课程设计