django框架之7:模板操作之URL标签与补充标签

URL标签:

在模版中,我们经常要写一些url,比如某个a标签中需要定义href属性。当然如果通过硬编码的方式直接将这个url写死在里面也是可以的。但是这样对于以后项目维护可能不是一件好事。因此建议使用这种反转的方式来实现,类似于django中的reverse一样。
在网页中的呈现就是通过链接进行页面跳转
通过:跳转,带参数页面的跳转和多个参数页面跳转的方式展开
首先:跳转的定义从HTML定义,语句:

<a href="/news/">进入新闻主页</a>  # 跳转到指定路由
<a href= "{% url 'index' %}" >进入新闻主页</a>   # 定义路由index  跳转到index中,此时即使路由地址改了,也不用再修改index了,比较方便

与{% url ‘index’ %}对应的是要从urls.py定义urls的值
path(’’,views.index,name=‘index’),

从views定义 index视图

def index(request):
    return render(request,'index.html')

如果要传入参数news_id:就需要在render内传入context值,在context内定义‘news_id’字段,
传入参数的方式分为位置传参与键值传参:

<a href= "{% url 'news_detail' 1 3 %}" >进入新闻详情页</a>  # 位置传参
<a href= "{% url 'news_detail' news_id=2 type_id=4 %}" >进入新闻2详情页</a>  # 键值传参

如果是多个参数,只需要用空格隔开,不需要用逗号隔开
如果需要传入参数get ?参数,需要在花括号外面操作,里面已经将url做好了连接反转,只需要进行连接即可
比如:

<a href= "{% url 'news_detail' news_id=2 type_id=4 %}?name=cheney" >进入新闻2详情页</a>

而在urls中是要写传递参数的,不然HTML也不知道怎么连接
比如:

path('news_detail/<news_id>/<type_id>/',views.news_detail,name='news_detail'),

注:在这一切操作开始之前,配置好settings中的'DIRS': [os.path.join(BASE_DIR,'templates')],
代码:
urls:

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

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

news.urls:

from django.urls import path
from . import views

urlpatterns = [
    path('',views.index,name='index'),
    path('news/',views.news,name='news'),
    path('news_detail/<news_id>/<type_id>/',views.news_detail,name='news_detail'),
]

news.views

from django.shortcuts import render
# 只使用render的渲染方式
# Create your views here.

def index(request):
    return render(request,'index.html')

def news(request):
    return render(request,'news.html')

def news_detail(request,news_id,type_id):
    name = request.GET.get('name')  #  不用动路由
    context = {
        'news_id':news_id,
        'type_id':type_id,
        'name':name
    }
    return render(request,'news_detail.html',context=context)

news.html:

from django.shortcuts import render
# 只使用render的渲染方式
# Create your views here.

def index(request):
    return render(request,'index.html')

def news(request):
    return render(request,'news.html')

def news_detail(request,news_id,type_id):
    name = request.GET.get('name')  #  不用动路由
    context = {
        'news_id':news_id,
        'type_id':type_id,
        'name':name
    }
    return render(request,'news_detail.html',context=context)

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <p>图书首页</p>
</body>
</html>

news_detail.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<p>新闻详情页面</p>
<p>这是第{{ news_id }}条新闻</p>
<p>这是第{{ type_id }}条关于{{ name }}的新闻</p>
</body>
</html>

操作:
在这里插入图片描述
此时,点击其中的页面,会自动跳转到相关的页面

在这里插入图片描述

with标签:

上下文管理
只能在当前的with标签里使用才有效果

<p>
    {% with name=person.0 %}
    {{ name }}
    {% endwith %}
    {% with person.1 as name%}
    {{ name }}
    {% endwith %}
    {{ name }}
</p>

其中包含with的两种写法,但是在结束with之后,name不再起作用

关闭转义

{% autoescape off  %}

对于:

context = {
        'person':['cheney','jerry'],
        'net': '<a href="https://www.baidu.com">百度</a>',
    }
<p>
    {{ net }}
    {% autoescape off %}
    {{ net }}
    {% endautoescape %}
</p>

如果直接输出,则输出的就是

<a href="https://www.baidu.com">百度</a>

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值