• Django框架路由的无名分组


    """mysite URL Configuration

    The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/3.2/topics/http/urls/
    Examples:
    Function views
    1. Add an import: from my_app import views
    2. Add a URL to urlpatterns: path('', views.home, name='home')
    Class-based views
    1. Add an import: from other_app.views import Home
    2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
    Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
    """
    from django.contrib import admin
    from django.urls import path,re_path
    from views.py import get_ncov,article,articleByMonth
    urlpatterns = [
    #urls路由控制器
    path('article/2012', article),
    re_path('article/\d+', article),
    re_path('^article/\d+$', article),
    re_path('^article/\d{4}$', article),
    #无名分组
    # /article/2012
    re_path('^article/(\d{4})$', article), # article(request,2012)
    re_path('^article/(\d{4})/\d{1,2}$', articleByMonth), # articleByMonth(request,2012,12)

    path('admin/', admin.site.urls),
    path('2019ncov/', get_ncov),

    '''
    re.findall('article/\d+',"article/2011abc")
    re.findall('article/\d+',"abc/article/2011abc")
    re.findall('article/\d+',"abc/article/abc/2011abc")
    '''

    ]
    from django.shortcuts import HttpResponse,render,redirect
    import datetime
    import requests
    def get_ncov(request):
    res = requests.get("https://2019ncov.chinacdc.cn/JKZX/yq_20220401.json")
    #print(res.json)
    data = res.json()["features"]
    return render(request,"ncov.html"),{"data":data}

    def article(request,year):
    # "select * from article where year = 2012"
    return HttpResponse("article文章列表" + year)

    def articleByMonth(request,year,month):
    # "select * from article where year = 2012"
    return HttpResponse(f"{year}年{month}月articleByMonth文章列表")

  • 相关阅读:
    MSSQL慢查询查询与统计
    SQLServer统计采集数据库相关信息
    python3 安装pymssql失败 pip3 install pymssql
    CentOS7源码安装Python3
    python3 安装pyodbc失败 pip3 install pyodbc
    Django配置Mysql数据库 (Pycharm)
    DBArtist之Oracle入门第4步: Oracle创建数据库
    DBArtist之Oracle入门第3步: 安装配置PL/SQL Developer
    linux三剑客
    python + SMTP 发送邮件
  • 原文地址:https://www.cnblogs.com/A121/p/16448449.html
Copyright © 2020-2023  润新知