• python测试开发django(4)--获取url参数和name的作用


    前言

    如打开博客园按时间分类标签页

    https://www.cnblogs.com/canglongdao/archive/2020/07.html,里面是时间参数是动态的,如果我想获取里面的时间参数2020和07这两个参数,这就涉及到url参数的获取了。

    获取url参数

    先用path去匹配一个url地址,类似于:archive/2020/07.html,于是取两个参数名称year,month

    参数用<name>这种格式

    #helloworld/helloworld/urls.py
    from django.conf.urls import url
    from django.urls import re_path,path
    from xjyn import views
    
    urlpatterns=[
       path("archive/<year>/<month>.html",views.hh),
    ]
    

     xjyn.py/views.py视图函数内容

    from django.shortcuts import render
    from django.http import HttpResponse,Http404
    # Create your views here.
    
    def hh(request,year="2020",month="07"):
        return HttpResponse("获取当前页面的时间标签{}年/{}月".format(year,month))
    

     启动服务后,浏览器输入地址: http://127.0.0.1:8000/archive/2020/7.html

     正则匹配url

    上面的案例虽然可以实现从url上获取参数了,但是会遇到一个问题,年和月可以输入各种数据,如:archive/2020/101.html,很显然不太合理。

    如果想让year参数只能是4个数字,month参数只能是2个数字,该怎么做呢?这就需要用到正则匹配了。

    • ?p 参数year
    • [0-9] 匹配0-9的数字
    • {4} 匹配4个数字
    • {1,2} 匹配1-2个数字
    • r是raw原型,不转义
    • ^ 匹配开始
    • $ 匹配结束
    #helloworld/helloworld/urls.py
    from django.conf.urls import url
    from django.urls import re_path,path
    from xjyn import views
    
    urlpatterns=[
       path("archive/<year>/<month>.html",views.hh),
        url(r'^archive1/(?P<year>[0-9]{4})/(?P<month>[0-9]{1,2}).html$',views.hh1),
    ]
    

     xjyn.py/views.py视图函数内容

    from django.shortcuts import render
    from django.http import HttpResponse,Http404
    # Create your views here.
    
    def hh(request,year="2020",month="07"):
        return HttpResponse("获取当前页面的时间标签{}年/{}月".format(year,month))
    def hh1(request,year="2020",month="07"):
        return HttpResponse("获取当前页面的时间标签{}年/{}月".format(year,month))
    

     启动服务后,浏览器输入地址:http://127.0.0.1:8000/archive1/2020/11.htmlhttp://127.0.0.1:8000/archive1/2020/1.html 月份输入2位数字或1位数字都可以。

    月份输入3位数字,报错;

     urls.py中定义name的作用

     如果现在有一个a.html页面,还有一个b.html页面,之前两个页面是独立的不想干的,如果现在需要从a页,点个按钮,跳转到b.html该如何实现?

    xjyn/templates/a.html写入以下内容;

    <base href="http://127.0.0.1:8000/",target="_blank">这一句化,必须写上,不然跳转的b页面时,地址异常(/a/b);
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>武汉-会</title>
    
        <base href="http://127.0.0.1:8000/",target="_blank">
    </head>
    <body>
    <p>
        欢迎学习django!
        <br>
        <a href="b/">点这里到b页</a>
    </p>
    </body>
    </html>
    

     xjyn/templates/b.html写如以下内容

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>b页面</title>
    </head>
    <body>
    <p>
        <h4>这是我的博客,可以百度搜:星空6</h4>
    <p>越努力,越幸运</p>
    
    </p>
    </body>
    </html>
    

     xjyn/views.py文件;函数名称可以随意命名,只要urls.py中,写入对应的函数名即可;

    from django.shortcuts import render
    from django.http import HttpResponse,Http404
    # Create your views here.
    
    def he(request):
        return render(request,"a.html")
    def hehe(request):
        return render(request,"b.html")
    

     xjyn/urls.py文件内容;url中的路径可以随意命名,访问时,根据路径访问即可;

    #helloworld/helloworld/urls.py
    from django.conf.urls import url
    from django.urls import re_path,path
    from xjyn import views
    
    urlpatterns=[
        url('^b/$',views.hehe),
        url('^a/$',views.he),
    
    ]
    

     如果在页面把url地址写死了:

    <a href="b/">点这里到b页</a>,这样会有个弊端,当多个页面用到这个地址时候,如果后续这个地址变了,那就很难维护了。
    为了url地址维护起来方便,可以给他起个唯一的名称,也就是name参数,接下来在url配置里加个name名称。
    name名称可随意命名,网页中与其保持一致即可;
    #helloworld/helloworld/urls.py
    from django.conf.urls import url
    from django.urls import re_path,path
    from xjyn import views
    
    urlpatterns=[
        url('^okk/$', views.hehe,name="b_pages"),
        url('^a/$', views.he,name="a_pages"),
    
    ]
    

     把xjyn/templates/a.html跳转的地址改成如下:

    <a href="{% url 'b_pages' %}">b页面</a>
    

      

    越努力,越幸运!!! good good study,day day up!!!
  • 相关阅读:
    CodeForces 1025G Company Acquisitions
    Luogu P4271 [USACO18FEB]New Barns P
    Luogu P1625 求和
    SP1772 Find The Determinant II
    CodeForces 1408G Clusterization Counting
    CodeForces 1420E Battle Lemmings
    设计模式——工厂模式
    设计模式——适配器模式
    rabbitMQ centos7 的安装
    dubbo spring 的使用
  • 原文地址:https://www.cnblogs.com/canglongdao/p/14092586.html
Copyright © 2020-2023  润新知