• 分页传参数的两种形式,url正则 ?id=1


    目的: 打开http://127.0.0.1:8000/home    点击查看详情转到 http://127.0.0.1:8000/detail-1-1.html实现查看具体信息

    一,利用url路由系统的正则表达式传参数

      url(r'^detail-(?P<uid>d+)-(?P<nid>d+).html',views.detail),

      def detail(request,uid,nid):

        return

    urls:

    from django.contrib import admin
    from django.urls import path
    from cmbd import views
    from django.conf.urls import url
    urlpatterns = [
        path('admin/', admin.site.urls),
        path('home/', views.home),
        path('home', views.home),
        url(r'^detail-(?P<uid>d+)-(?P<nid>d+).html',views.detail),
    ]
    

    views:

    from django.shortcuts import render,redirect
    user_dict = {
        '1':{'name':'hrg','email':'hrg@163.com','pwd':'123'},
        '2':{'name':'eric','email':'hrg@163.com','pwd':'111'},
        '3':{'name':'hhh','email':'hrg@163.com','pwd':'222'},
        '4':{'name':'kkk','email':'hrg@163.com','pwd':'123'},
    }
    
    def home(request):
        return render(request,'home.html',{'user_dict':user_dict,})
    
    def detail(request,uid,nid):
        detail_dict = user_dict[uid]
        print(detail_dict)
        return render(request,'detail.html',{'detail_dict':detail_dict,})
    

    templates:

    home.html
    
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
        <ul>
            {% for k,v in user_dict.items %}
                <li>
                    <span>id:{{ k }}姓名:{{ v.name }}</span>
                    <a href="/detail-{{ k }}-1.html">查看详情</a>
                </li>
            {% endfor %}
        </ul>
    </body>
    </html>
    
    
    detail.html
    
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
        <h1>用户详情:</h1>
        <ul>
            {% for k,v in detail_dict.items %}
                <li>{{ k }}:{{ v }}</li>
            {% endfor %}
        </ul>
    </body>
    </html>
    

    二,利用URL地址栏?功能传参数  

      /detail?uid=1&pid=2

    urls:

    from django.contrib import admin
    from django.urls import path
    from cmbd import views
    from django.conf.urls import url
    urlpatterns = [
        path('admin/', admin.site.urls),
        path('home/', views.home),
        path('home', views.home),
        path('detail',views.detail),
    ]
    

    views:

    from django.shortcuts import render,redirect
    user_dict = {
        '1':{'name':'hrg','email':'hrg@163.com','pwd':'123'},
        '2':{'name':'eric','email':'hrg@163.com','pwd':'111'},
        '3':{'name':'hhh','email':'hrg@163.com','pwd':'222'},
        '4':{'name':'kkk','email':'hrg@163.com','pwd':'123'},
    }
    
    def home(request):
        return render(request,'home.html',{'user_dict':user_dict,})
    
    def detail(request):
        uid = request.GET.get('uid')   #获取home.html中/detail?uid={{ k }}传来的值
        detail_dict = user_dict[uid]
        print(detail_dict)
        return render(request,'detail.html',{'detail_dict':detail_dict,})
    

    templates:

    home.html
    
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
        <ul>
            {% for k,v in user_dict.items %}
                <li>
                    <span>id:{{ k }}姓名:{{ v.name }}</span>
                    <a href="/detail?uid={{ k }}">查看详情</a>
                </li>
            {% endfor %}
        </ul>
    </body>
    </html>
    
    detail.html
    
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
        <h1>用户详情:</h1>
        <ul>
            {% for k,v in detail_dict.items %}
                <li>{{ k }}:{{ v }}</li>
            {% endfor %}
        </ul>
    </body>
    </html>
    

      



  • 相关阅读:
    mysql表检查分析优化
    mysql表存储
    mysql表空间文件
    mysql回滚日志
    mysql重做日志
    mysql二进制日志
    最佳高质量字体
    mysql存储引擎
    如何提取app软件的apk格式中的字体?
    tar split命令
  • 原文地址:https://www.cnblogs.com/alex-hrg/p/9674762.html
Copyright © 2020-2023  润新知