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)}")
相关推荐
小熊Coding1 天前
Windows 上安装 mysqlclient 时遇到了编译错误,核心原因是缺少 Microsoft Visual C++ 14.0 或更高版本 的编译环境。
c++·windows·python·microsoft·django·mysqlclient·bug记录
深兰科技1 天前
深兰科技与宝武集团旗下钢友汇达成国际市场合作,俄罗斯、巴西、阿根廷市场同步推进
人工智能·django·fastapi·pygame·httpx·视觉大模型·深兰科技
betazhou1 天前
django+postgresql创建第一个网站
python·postgresql·django
二等饼干~za8986682 天前
云罗 GEO 优化系统源码厂家测评报告
大数据·网络·数据库·人工智能·django
高山流水&上善2 天前
基于Qwen3-0.6B的光纤基础测试问答系统设计与实现
python·django
AC赳赳老秦3 天前
OpenClaw email技能:批量发送邮件、自动回复,高效处理工作邮件
运维·人工智能·python·django·自动化·deepseek·openclaw
龙腾AI白云4 天前
多模大模型应用实战:智能问答系统开发
python·机器学习·数据分析·django·tornado
程序媛徐师姐4 天前
Python基于OpenCV的马赛克画的设计与实现【附源码、文档说明】
python·opencv·django·马赛克绘画·python马赛克绘画系统·马赛克画·python马赛克画
斯班奇的好朋友阿法法4 天前
Django 项目打包部署完整指南(适配你的项目,零报错)
python·django·sqlite
斯班奇的好朋友阿法法4 天前
Django 3.2 项目:从 Hello World 开始(完整功能版)
python·django