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)}")
相关推荐
lzptouch1 天前
Django项目
后端·python·django
Anson Jiang2 天前
PyTorch轻松实现CV模型:零基础到实战
pytorch·python·django·flask·python开发
Python私教4 天前
基于 Django 5 + DRF 构建博客系统后端接口(从建模到接口实现)
python·django·sqlite
IT学长编程4 天前
计算机毕业设计 基于Python的热门游戏推荐系统的设计与实现 Django 大数据毕业设计 Hadoop毕业设计选题【附源码+文档报告+安装调试】
大数据·python·django·毕业设计·课程设计·毕业论文
rexling14 天前
【玩转全栈】----Django基本配置和介绍
数据库·django·sqlite
IT学长编程4 天前
计算机毕业设计 基于Python的电商用户行为分析系统 Django 大数据毕业设计 Hadoop毕业设计选题【附源码+文档报告+安装调试】
大数据·hadoop·python·django·毕业设计·课程设计·电商用户行为分析系统
maotou5265 天前
dvadmin开发文档(第一版)
python·django
硬件人某某某6 天前
python基于卷积神经网络的桥梁裂缝检测系统(django),附可视化界面,源码
python·cnn·django
小Pawn爷6 天前
构建Django的Web镜像
前端·python·docker·django
初学者,亦行者6 天前
Rust 模式匹配的穷尽性检查:从编译器证明到工程演进
后端·rust·django