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)}")
相关推荐
Mr数据杨1 天前
【Codex】用学生考试初始化模块批量准备考试基础数据
django·codex·项目开发
典典分享指南1 天前
Excel SUMIFS 跨表汇总实战:多表条件求和的完整指南
django·flask·numpy·pyqt·fastapi
BYSJMG1 天前
计算机毕设选题|基于大数据与文本挖掘的自动驾驶事故成因分析及可视化研究|Hadoop+PySpark+Django+Vue
大数据·hadoop·信息可视化·django·自动驾驶·kmeans·课程设计
Mr数据杨2 天前
【Codex】用下载中心模块管理系统导出文件与下载任务
django·codex·项目开发
171320330计算机毕设编程2 天前
2027计算机毕设五大方向对比&选题推荐
java·ide·python·算法·django·php·推荐算法
青 春 记 忆2 天前
零基础入门python43:Django接口测试与库存边界
python·django·后端开发
Mr数据杨2 天前
【Codex】用智能助手问题反馈模块收集AI使用问题
人工智能·django·codex·项目开发
ikun_文3 天前
Django框架路由Router的使用
python·pycharm·django
我的xiaodoujiao4 天前
Django 基础知识详细图文教程 4-Django 视图定义与使用
开发语言·数据库·后端·测试工具·django·sqlite
2601_962180334 天前
python——Django 框架
开发语言·python·django