Django实现点击详情跳转新界面、路由名称和分发3

一、url中get方式

利用url中get传参,打开一个新页面

views.py中定义函数,并在urls填写好路由信息:

def index(request):
    return render(request,'index.html',{'user':USER_DICT})
def detail(request):
    #点击跳转详细界面,在url中get方式传参数
    nid=request.GET.get('nid')
    detail_info=USER_DICT[nid]
    return render(request,'detail.html',{'detail_info':detail_info})
urlpatterns = [
path('index/', views.index),
path('detail/',views.detail)
]

index.html中:

   <ul>
{#         点击跳转详细界面,url中传参#}
        {% for foo,v in user.items %}
        <li><a target="_blank" href="/detail/?nid={{ foo }}">{{ v.name }}</a></li>
        {% endfor %}
    </ul>

detail.html中:

<h1>详细信息</h1>
<h4>用户名:{{ detail_info.name }}</h4>
<h4>是否为超级管理员:{{ detail_info.super }}</h4>

二、利用正则匹配

1.基础方法:

一个类url对应一个函数

在views.py和urls.py中修改好:

def index(request):
    return render(request,'index.html',{'user':USER_DICT})
def detail(request,nid):
    #一类url对应一个函数
    detail_info=USER_DICT[nid]
    return render(request,'detail.html',{'detail_info':detail_info})

此处是在新建的APP文件中新建一个urls.py中修改

from django.conf.urls import url
from . import views
urlpatterns = [
    url('login/', views.login),
    url('home/',views.Home.as_view()),
    url('index/', views.index),
    # url('detail/',views.detail),
    url(r'detail-(\d+)', views.detail, name="index"),          #一类url对应一个函数
]

          index.html中修改:

   <ul>
{#         一个类的url对应一个函数#}
        {% for foo,v in user.items %}
        <li><a target="_blank" href="/detail-{{ foo }}.html">{{ v.name }}</a></li>
        {% endfor %}
    </ul>

detail.html不做修改

2.规范写法(推荐写法)

views.py和urls.py中,分别修改detail的形参和路径中的正则表达式:

def detail(request,*args,**kwargs):
    #一类url对应一个函数,传列表和字典
    nid=kwargs['nid']
    detail_info=USER_DICT[nid]
    return render(request,'detail.html',{'detail_info':detail_info})
urlpatterns = [
    url('login/', views.login),
    url('home/',views.Home.as_view()),
    url('index/', views.index),
    # url('detail/',views.detail),
    # url(r'detail-(\d+)-(\d+).html', views.detail, name="index"),          #一类url对应一个函数
    url(r'detail-(?P<nid>\d+)-(?P<pid>\d+).html', views.detail),          #一类url对应一个函数
]

index.html可以不做修改,这里为了便于观察结果,稍作调整:

    <ul>
{#         点击跳转详细界面,url中传参#}
        {% for foo,v in user.items %}
        <li><a target="_blank" href="/detail-{{ foo }}-{{ foo }}.html">{{ v.name }}</a></li>
        {% endfor %}
    </ul>

三、路由的对应名称

路由对应的是url中的路径信息,Django提供了便于修改其路径信息的方法

1.初级使用

在urls.py中定义,增添name属性:

urlpatterns = [
    url('login/', views.login),
    url('home/',views.Home.as_view()),
    url('myindex/', views.index,name='index'),
]

模板语言中调用,修改action的提交路径,注意空格:

<form action="{% url 'index' %}" method="post">
    <p><input type="text" placeholder="用户名" name="user"/></p>
    <p><input type="text" placeholder="是否为管理员" name="super"/></p>
    <input type="submit" value="提交" />
</form>

2.在当前url中提交到指定url路径下

views.py和urls.py中依次修改相应的函数:

def index(request,nid):
    #需要接收获取到的数字
    return render(request,'index.html',{'user':USER_DICT})
urlpatterns = [
    url('myindex/(\d+)', views.index,name='index')
]

模板语言中修改action(这里只是增加数字1):

<form action="{% url 'index' 1 %}" method="post">
    <p><input type="text" placeholder="用户名" name="user"/></p>
    <p><input type="text" placeholder="是否为管理员" name="super"/></p>
    <input type="submit" value="提交" />
</form>

3.在当前url中提交到当前url路径

views.py和urls.py中的信息同上,这里只需要修改模板语言中的action:

<form action="{{ request.path_info }}" method="post">
    <p><input type="text" placeholder="用户名" name="user"/></p>
    <p><input type="text" placeholder="是否为管理员" name="super"/></p>
    <input type="submit" value="提交" />
</form>

4.提交当前url到指定url路径的其他写法:

4.1 在views.py中直接修改request.path_info的值

def index(request,nid):
    print(request.path_info)
    # 1.直接写死,在这里修改path_info的值
    request.path_info='/myindex/40'
    return render(request, 'index.html', {'user': USER_DICT})

4.2 在views.py中调用函数,执行django.urls的reverse函数

a.对于列表型的参数:

def index(request,nid):
    print(request.path_info)
    # 1.直接写死,在这里修改path_info的值
    # request.path_info='/myindex/40'
    #2.方法2
    from django.urls import reverse
    v=reverse('index',args=(40,))
    request.path_info=v
    return render(request,'index.html',{'user':USER_DICT})

模板语言index.html中不做修改,这里仅做展示

<form action="{{ request.path_info }}" method="post">
    <p><input type="text" placeholder="用户名" name="user"/></p>
    <p><input type="text" placeholder="是否为管理员" name="super"/></p>
    <input type="submit" value="提交" />
</form>

b.对于字典型的参数:

首先应在urls.py中定义:

urlpatterns = [
    url('login/', views.login),
    url('home/',views.Home.as_view()),
    url('myindex/(?P<nid>\d+)/(?P<pid>\d+)', views.index,name='index'),
]

然后在views.py中修改:

def index(request,*args,**kwargs):
    print(request.path_info)
    # 1.直接写死,在这里修改path_info的值
    # request.path_info='/myindex/40'
    #2.方法2
    from django.urls import reverse
    v=reverse('index',kwargs={"nid":1,"pid":99})
    request.path_info=v
    return render(request,'index.html',{'user':USER_DICT})

index.html中不做修改,这里仅做展示:

<form action="{{ request.path_info }}" method="post">
    <p><input type="text" placeholder="用户名" name="user"/></p>
    <p><input type="text" placeholder="是否为管理员" name="super"/></p>
    <input type="submit" value="提交" />
</form>

5.在模板语言中修改url提交的路径

action="{% url 'index' %}"
action="{% url 'index' 1  92 %}"
action="{% url 'index' nid=1  pid=92 %}"

四、路由分发

1.多级路由

在project下的urls.py中修改:

from django.contrib import admin
from django.urls import path,include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('',include('app01.urls')),
]

在app目录下创建一个urls.py中修改:

from django.conf.urls import url
from . import views
urlpatterns = [
    url('login/', views.login),
    url('home/',views.Home.as_view()),
    url('myindex/(?P<nid>\d+)/(?P<pid>\d+)', views.index,name='index'),
]

2.命名空间

在project的urls.py中修改:

from django.contrib import admin
from django.urls import path,include

urlpatterns = [
    path('admin/', admin.site.urls),
    path(r'shop/', include('shop.urls', namespace='shop-polls')),
    path(r'consumer/', include('consumer.urls', namespace='consumer-polls')),
] 

分别修改相应的app文件的urls.py:

from django.conf.urls import url
from . import views
app_name = 'shop'
urlpatterns = [
    url(r'^(?P<nid>\d+)/$', views.detail, name='detail')
]

修改对应app文件下的views.py文件

def detail(request, nid):
    print(request.resolver_match)
    return HttpResponse(nid)

           定义带命名空间的url之后,使用name生成URL时候,应该如下,按照相应的方式来填写:

v = reverse('shop:detail', kwargs={'nid':11})
<form action="{% url 'shop:detail' nid=12 %}" method="post">
    <p><input type="text" name="user" placeholder="请输入用户名:"/></p>
    <input type="submit" value="提交数据"/>
</form>
<form action="{% url 'consumer:detail' nid=16 %}" method="post">
    <p><input type="text" name="user" placeholder="请输入用户名:"/></p>
    <input type="submit" value="提交数据"/>
</form>

 

  • 0
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

theskylife

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值