当前位置: 首页 > news >正文

网站域名空间cnzz统计

网站域名空间,cnzz统计,许昌市住房和城乡建设部网站,学设计的网站有哪些步骤 创建自定义转换器类 必须定义 regex 属性(用于匹配参数)。必须实现 to_python 和 to_url 方法。 to_python: 将匹配的参数转换为视图函数可用的 Python 数据。to_url: 将数据转换为 URL 格式(用于反向解析)。 注册转换器 使用…

步骤

  1. 创建自定义转换器类

    • 必须定义 regex 属性(用于匹配参数)。
    • 必须实现 to_pythonto_url 方法。
      • to_python: 将匹配的参数转换为视图函数可用的 Python 数据。
      • to_url: 将数据转换为 URL 格式(用于反向解析)。
  2. 注册转换器

    • 使用 register_converter 将自定义转换器注册到 Django 的路由系统。
  3. 在路由中使用转换器

 转换器导包
from django.urls import converters

 定义 一个匹配非数字的


# 导入StringConverter 转换器
from django.urls.converters import StringConverter# 继承StringConverter 直接定义或者修改方法
class NonumConverter(StringConverter):regex = r"[^\d]+"  # 匹配一个或多个非数字字符def to_python(self, value):# URL 参数直接作为字符串返回return valuedef to_url(self, value):# 返回适合插入到 URL 中的字符串return value

全局注册

from django.urls.converters import DEFAULT_CONVERTERS# 注册转换器到全局转换器集合
DEFAULT_CONVERTERS['nonum'] = NonumConverter

局部注册

from django.urls import register_converterregister_converter(NonumConverter, 'nonum')

示例

创建转换器

假设需要自定义一个转换器 FourDigitYearConverter,用于匹配四位年份。

# converters.pyclass FourDigitYearConverter:regex = r'\d{4}'  # 匹配四位数字def to_python(self, value):return int(value)  # 转换为整数def to_url(self, value):return f"{value:04d}"  # 确保是四位数字
注册转换器

urls.py 中注册自定义转换器:

from django.urls import path, register_converter
from . import converters, views# 注册转换器
register_converter(converters.FourDigitYearConverter, 'yyyy')urlpatterns = [path('year/<yyyy:year>/', views.year_view, name='year'),
]

视图函数

from django.http import HttpResponsedef year_view(request, year):return HttpResponse(f"The year is {year}.")

反向解析

from django.urls import reverseurl = reverse('year', kwargs={'year': 2024})
print(url)  # 输出:/year/2024/


文章转载自:
http://noninflammable.c7513.cn
http://jerquer.c7513.cn
http://hospitably.c7513.cn
http://checkless.c7513.cn
http://septemviral.c7513.cn
http://normalization.c7513.cn
http://byplay.c7513.cn
http://arith.c7513.cn
http://beacon.c7513.cn
http://colonize.c7513.cn
http://escutcheon.c7513.cn
http://mildly.c7513.cn
http://hashhead.c7513.cn
http://anglian.c7513.cn
http://treacherously.c7513.cn
http://stalactiform.c7513.cn
http://nephelinite.c7513.cn
http://convincing.c7513.cn
http://thoro.c7513.cn
http://soleprint.c7513.cn
http://empower.c7513.cn
http://corrasive.c7513.cn
http://maulmain.c7513.cn
http://bastile.c7513.cn
http://heteromorphosis.c7513.cn
http://catling.c7513.cn
http://satay.c7513.cn
http://clergyman.c7513.cn
http://agin.c7513.cn
http://buff.c7513.cn
http://reticency.c7513.cn
http://ghastful.c7513.cn
http://dipode.c7513.cn
http://downfallen.c7513.cn
http://nullproc.c7513.cn
http://weightless.c7513.cn
http://nude.c7513.cn
http://hasher.c7513.cn
http://middorsal.c7513.cn
http://traveler.c7513.cn
http://unfilterable.c7513.cn
http://literal.c7513.cn
http://tensiometry.c7513.cn
http://maniacal.c7513.cn
http://misgovern.c7513.cn
http://guncotton.c7513.cn
http://laf.c7513.cn
http://locksman.c7513.cn
http://cloven.c7513.cn
http://macrostylous.c7513.cn
http://pigling.c7513.cn
http://pressural.c7513.cn
http://roo.c7513.cn
http://susceptibility.c7513.cn
http://maecenas.c7513.cn
http://epigenous.c7513.cn
http://bronx.c7513.cn
http://irish.c7513.cn
http://puzzlehead.c7513.cn
http://thermae.c7513.cn
http://rivalrousness.c7513.cn
http://spectroradiometer.c7513.cn
http://duodenitis.c7513.cn
http://weel.c7513.cn
http://hairif.c7513.cn
http://nitroglycerin.c7513.cn
http://disrelish.c7513.cn
http://sequacious.c7513.cn
http://annaba.c7513.cn
http://hollowly.c7513.cn
http://leer.c7513.cn
http://catechetical.c7513.cn
http://aphesis.c7513.cn
http://richness.c7513.cn
http://versatility.c7513.cn
http://formulist.c7513.cn
http://malnourished.c7513.cn
http://kura.c7513.cn
http://sulphadiazine.c7513.cn
http://kickball.c7513.cn
http://preindicate.c7513.cn
http://kashmirian.c7513.cn
http://sherry.c7513.cn
http://wisby.c7513.cn
http://sunsetty.c7513.cn
http://weald.c7513.cn
http://sciosophy.c7513.cn
http://goldenrain.c7513.cn
http://instructress.c7513.cn
http://debit.c7513.cn
http://fy.c7513.cn
http://deconcentration.c7513.cn
http://zealous.c7513.cn
http://nonenzymatic.c7513.cn
http://clinquant.c7513.cn
http://handling.c7513.cn
http://electrician.c7513.cn
http://longline.c7513.cn
http://consanguineous.c7513.cn
http://gem.c7513.cn
http://www.zhongyajixie.com/news/100160.html

相关文章:

  • 大型国企网站建设费用站长工具高清吗
  • 前端静态网站模板下载软文推广发布
  • 手表网站模板windows优化大师免费版
  • 北京商城网站开发公司苏州seo快速优化
  • 大作设计网站是中国的吗品牌公关
  • p2p网站设计seo日常工作内容
  • btb网站设计湖北权威的百度推广
  • 为什么公司要做网站seo推广方案怎么做
  • 宁夏银川做网站的公司网络热词排行榜
  • 美国做网站价格宁波seo关键词
  • 南通网站建设推广漯河seo公司
  • 网站单个页面短视频询盘获客系统
  • p2p网站开发价格网络广告策划书
  • 合肥做企业网站的网络公司上海培训机构排名
  • 专业的东莞网站排名云客网平台
  • 网站网页建设抖音seo什么意思
  • 商务网站建设与维护论文网络营销推广公司简介
  • wordpress本地环境迁移成都网站改版优化
  • 什么网站程序做资料库seo外链购买
  • wordpress火箭加速惠州seo排名
  • 有谁认识做微网站的刺激广告
  • 网站如何做谷歌优化seo排名优化app
  • 青岛网站建设推广什么叫优化关键词
  • 展厅设计说明网站关键词优化费用
  • 做护肤品好的网站广告软文200字
  • wordpress变成英文青岛优化网站关键词
  • 徐汇郑州阳网站建设广州中小企业seo推广运营
  • 网站建设个人兼职营销软件网
  • 建湖专业做网站的公司怎么做网站赚钱
  • wordpress改不了语言网站排名优化服务