zoukankan      html  css  js  c++  java
  • Django入门三之urls.py重构及参数传递

    1. 内部重构

    2. 外部重构

    website/blog/urls.py

     

    website/website/urls.py

    3. 两种参数处理方式 

     -1. blog/index/?id=1234&name=bikmin

       #urls.py

    url(r'^blog/index/$','get_id_name')

        #views.py

    from django.http import HttpResponse
    from django.template import loader,Context
    
    def get_id_name(request):
        html = loader.get_template("index.html")
        id = request.GET.get("id")
        name = request.GET.get("name")
        context = Context({"id":id,"name":name})
        return HttpResponse(html.render(context))

        #index.html

    <body>
        <li>id:{{ id }}</li>
        <li>name:{{ name }}</li>
    </body>

       效果如下

     

     -2. blog/index/1234/bikmin

      #urls.py

    url(r'^blog/index/(d{4})/(w+)/$','blog.views.get_id_name')

       #views.py

    from django.http import HttpResponse
    from django.template import loader,Context
    
    def get_id_name(request,p1,p2):
        html = loader.get_template("index.html")
        context = Context({"id":p1,"name":p2})
        return HttpResponse(html.render(context))

       #index.html

    <body>
        <li>id:{{ id }}</li>
        <li>name:{{ name }}</li>
    </body>

      效果如下:

     

      -3. blog/index/1234/bikmin  (和-2不一样的在于views.py,接收的参数名是限定的)

      #urls.py

    #限定id,name参数名
    url(r'blog/index/(?P<id>d{4})/(?P<name>w+)/$','get_id_name')

      #views.py

    from django.http import HttpResponse
    from django.template import loader,Context
    
    def get_id_name(request,id,name):
        html = loader.get_template("index.html")
        context = Context({"id":id,"name":name})
        return HttpResponse(html.render(context))

      #index.html

    <body>
        <li>id:{{ id }}</li>
        <li>name:{{ name }}</li>
    </body>

      效果如下

  • 相关阅读:
    jemeter使用笔记
    webtest mobile + Android / Battery Historian / SoloPi
    script / go / golang
    my live house / air conditioning / dajin / dakin / FTXG50JV2CW
    OS + Centos OS 8 Thinkpad TrackPoint
    network router Gpon
    Mybatisplus 自定义sql 使用条件构造器 多表查询分页
    处理fastJson 序列化时间问题
    js 实现轮播图 超级简单 组件已封装
    Webpack-dev-server的proxy用法
  • 原文地址:https://www.cnblogs.com/wongbingming/p/6900593.html
Copyright © 2011-2022 走看看