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)}")
相关推荐
龙腾AI白云16 小时前
多模大模型应用实战:智能问答系统开发
python·机器学习·数据分析·django·tornado
程序媛徐师姐18 小时前
Python基于OpenCV的马赛克画的设计与实现【附源码、文档说明】
python·opencv·django·马赛克绘画·python马赛克绘画系统·马赛克画·python马赛克画
斯班奇的好朋友阿法法20 小时前
Django 项目打包部署完整指南(适配你的项目,零报错)
python·django·sqlite
斯班奇的好朋友阿法法20 小时前
Django 3.2 项目:从 Hello World 开始(完整功能版)
python·django
源码之屋2 天前
计算机毕业设计:Python天气数据采集与可视化分析平台 Django框架 线性回归 数据分析 大数据 机器学习 大模型 气象数据(建议收藏)✅
人工智能·python·深度学习·算法·django·线性回归·课程设计
架构师老Y2 天前
003、Python Web框架深度对比:Django vs Flask vs FastAPI
前端·python·django
暴力袋鼠哥3 天前
基于 Django 与 Vue 的汽车数据分析系统设计与实现
vue.js·django·汽车
360智汇云3 天前
PostgreSQL 全文检索深度指南:内置 FTS、zhparser 与 pg_search 全解
postgresql·django·全文检索
leo_messi944 天前
2026版商城项目(三)-- ES+认证服务
后端·python·django
毕胜客源码4 天前
改进yolov8的香蕉成熟度检测系统,改进前后的模型指标对比,有技术文档,支持图像、视频和摄像实时检测
人工智能·python·深度学习·yolo·django