加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

django-URL别名的作用(六)

发布时间:2020-12-15 17:06:51 所属栏目:大数据 来源:网络整理
导读:接include函数那一节。 作用:为url地址取一个名称,这样在html中引用的时候,无论后台url怎么变,都可以访问到对应的界面,可以减少更改的次数。 基本目录: ? ?bookurls.py from django.urls import path from . viewsurlpatterns = [ path( '' ,views.in

接include函数那一节。

作用:为url地址取一个名称,这样在html中引用的时候,无论后台url怎么变,都可以访问到对应的界面,可以减少更改的次数。

基本目录:

?

?bookurls.py

from django.urls import path
from .  views

urlpatterns = [
    path('',views.index,name='index'),path(news/',views.news,1)">newsvideos/videos
from django.shortcuts  render
from django.http  HttpResponse

# Create your views here.
def index(request):
    return render(request,index.html)

 news(request):
    return HttpResponse(我是新闻的首页页面 videos(request):
    我是视频的首页页面')

booktemplatesindex.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset=UTF-8">
    <title>Title</title>
    <style>
        p{font-size: 28px;}
    </style>
</head>
<body>
<p><a href={% url '%}>index</a></p>
<p><a href={% url '%}>news</a></p>
<p><a href={% url '%}>videos</a></p>
</body>
</html>

当我们启动服务器后,会首先调用bookviews.py中的index函数,跳转到index.html

?

?点击news

?

?点击videos

?

?如果我们不取名字,那么在html中要用"http://localhost:8000/videos",这样虽然也有相同的作用,但是更改urls里面的path后,这里的同样也要更改,较为繁琐。

?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读