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)}")

相关推荐

  1. Django

    2024-03-22 20:20:03       39 阅读
  2. Django——

    2024-03-22 20:20:03       47 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-22 20:20:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-22 20:20:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-22 20:20:03       82 阅读
  4. Python语言-面向对象

    2024-03-22 20:20:03       91 阅读

热门阅读

  1. 开灯问题 C语言

    2024-03-22 20:20:03       45 阅读
  2. RabbitMQ如何实现延迟消息?

    2024-03-22 20:20:03       47 阅读
  3. 算法练习第三十天|两道hard51. N 皇后、37. 解数独

    2024-03-22 20:20:03       45 阅读
  4. C语言判断回文数

    2024-03-22 20:20:03       45 阅读
  5. 321——美团一面

    2024-03-22 20:20:03       42 阅读
  6. 【PMP】每日一练2

    2024-03-22 20:20:03       35 阅读
  7. MacOS - GCC 版本升级解决方案

    2024-03-22 20:20:03       42 阅读
  8. 蓝桥杯考试注意事项

    2024-03-22 20:20:03       54 阅读
  9. HarmonyOS状态管理:@State与@Prop、@Link的示例

    2024-03-22 20:20:03       31 阅读