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)}")
相关推荐
一 乐4 小时前
智慧社区|基于Python + Django智慧社区系统(源码+数据库+文档)
java·数据库·python·django·论文·毕设·智慧社区系统
源码之屋4 小时前
计算机毕业设计:Python天天基金数据采集与智能分析平台 Django框架 数据分析 可视化 爬虫 大数据 大模型(建议收藏)✅
人工智能·爬虫·python·数据分析·django·flask·课程设计
源码之家5 小时前
计算机毕业设计:Python基金股票数据分析与可视化平台 Django框架 数据分析 可视化 爬虫 大数据 大模型(建议收藏)✅
爬虫·python·信息可视化·数据分析·django·flask·课程设计
AC赳赳老秦14 小时前
OpenClaw二次开发实战:编写专属办公自动化技能,适配个性化需求
linux·javascript·人工智能·python·django·测试用例·openclaw
yuanmazhiwu1 天前
计算机毕业设计:Python股市行情可视化与ARIMA预测系统 Flask框架 ARIMA 数据分析 可视化 大数据 大模型(建议收藏)✅
大数据·python·数据分析·django·flask·课程设计
NotFound4861 天前
分享实战中Python Web 框架对比:Django vs Flask vs FastAPI
前端·python·django
羌俊恩1 天前
Centos环境django项目部署过程
django·flask·centos·mvc·mtv·web项目框架
源码之家1 天前
计算机毕业设计源码:京东商品数据采集分析可视化系统python Django Selenium爬虫 人工智能 大数据(建议收藏)✅
人工智能·爬虫·python·信息可视化·数据分析·django·课程设计
源码之家1 天前
计算机毕业设计:Python股票数据分析与ARIMA预测系统 Flask框架 ARIMA 数据分析 可视化 大数据 大模型(建议收藏)✅
大数据·python·数据挖掘·数据分析·django·flask·课程设计
北冥有羽Victoria1 天前
Django Auth组件完整版教程:从原理到项目落地
大数据·服务器·数据库·后端·python·django·sqlite