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)}")
相关推荐
Betelgeuse761 天前
告别传统 ModelForm:用 React 与 DRF 打造现代化项目管理表单
前端·react.js·django·前端框架
编程小风筝1 天前
就业信息推荐系统 Python+Django+Vue.js
vue.js·python·django
万粉变现经纪人2 天前
如何解决 pip install flash-attention 报错 需要 SM_80+(Ampere)架构 问题
python·架构·django·bug·virtualenv·pip·pygame
云和数据.ChenGuang2 天前
机器学习之方差和标准差计算
人工智能·python·机器学习·django·pygame·deepseek
开开心心就好2 天前
经典塔防游戏移植移动端随时畅玩
java·前端·科技·游戏·edge·django·pdf
小熊Coding3 天前
Windows 上安装 mysqlclient 时遇到了编译错误,核心原因是缺少 Microsoft Visual C++ 14.0 或更高版本 的编译环境。
c++·windows·python·microsoft·django·mysqlclient·bug记录
深兰科技3 天前
深兰科技与宝武集团旗下钢友汇达成国际市场合作,俄罗斯、巴西、阿根廷市场同步推进
人工智能·django·fastapi·pygame·httpx·视觉大模型·深兰科技
betazhou3 天前
django+postgresql创建第一个网站
python·postgresql·django
二等饼干~za8986684 天前
云罗 GEO 优化系统源码厂家测评报告
大数据·网络·数据库·人工智能·django
高山流水&上善4 天前
基于Qwen3-0.6B的光纤基础测试问答系统设计与实现
python·django