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白云2 小时前
数字孪生底层逻辑和技术
深度学习·django·flask·fastapi·tornado
AC赳赳老秦4 小时前
OpenClaw 全平台安装详解:Windows 10/11、macOS、Linux 零踩坑指南 (附一键脚本)
大数据·人工智能·python·django·去中心化·ai-native·openclaw
Mr数据杨7 小时前
【Dv3Admin】Vue3一键配置权限规则
django
Mr数据杨8 小时前
【Dv3Admin】Vue3通用自定义工作台卡片
django
云和数据.ChenGuang8 小时前
数据分析中的dataframe详解
python·数据挖掘·数据分析·django·pygame
Betelgeuse761 天前
DjangoBlog学习案例:掌握Django MVT架构与多应用协作实践
学习·架构·django
IT 行者1 天前
Claude Code Viewer: 打造 Web 端 Claude Code 会话管理利器
前端·人工智能·python·django
龙腾AI白云1 天前
数字孪生在航空领域的应用方法及案例
学习·django·virtualenv·pygame
q_35488851531 天前
计算机毕业设计源码:锦江酒店大数据分析与个性化推荐系统 Django框架 Vue 可视化 Hadoop 爬虫 协同过滤推荐算法 民宿 客栈(建议收藏)✅
python·机器学习·信息可视化·数据分析·django·课程设计·旅游
tryCbest1 天前
Django 基础入门教程(第四篇):Form组件、Auth认证、Cookie/Session与中间件
python·django