zoukankan      html  css  js  c++  java
  • 【Django入坑之路】基础操作(过滤,继承,跳转)

    1:自定过滤器

     1 1创建templatetags文件夹      
     2 2在里面创建自定义py文件:固定格式:
     3 from django import template
     4 from django.utils.safestring import mark_safe
     5 register = template.Library()
     6 @register.filter   +  def 自定义方法
     7 @register.simple_tag   +  def 自定义方法
     8 3 在需要的html文件中导入{{% load  创建的py文件名 %}}
     9 使用方法:
    10 @register.filter------------------------{{ 变量|函数名:参数 }}
    11 @register.simple_tag----------------{% 函数名 x,y,z   %}
    View Code

    2:继承模板

    1:创建新的html清空代码,填写:
    {% extends "继承的母文件名.html" %}
    2:在需要修改的地方加上:
    {% block 自定义文件名 %}
    “在这里复制母文件名里修改的地方进行修改”
    {{ block.super }}------可以拿到母文件修改的原内容
    {% endblock %}
    
    //继承模板02--include方法
    1:创建新的html,填写设计的样式
    2:在extends文件中,填写代码:
    {% load staticfiles %}----固定代码
    3:在需要添加设计样式的地方填写:
    {% include "设计样式的模板.html" %}
    View Code

    3:前端跳转请求

     1 1:在Django中,<a>标签的href属性可以直接跳转到某个路由页面,但是它要使用的是GET方法,所以一定要在路由函数里面写清楚GET方法跳转的制定页面:
     2 Def index(request):
     3 if request.method=="GET":
     4 //href跳转的路由一定要带着这个方法跳转
     5         return render(request,"add_classes.html")
     6     if request.method=="POST":
     7 Pass
     8 2:同一个页面:跳转用a标签HREF的GTE方法,提交数据组件用POST方法
     9 3:提交完数据后,如果想要跳转到其他页面要使用redirect(//路由)
    10 4:删除指定某条的时候(要拿到那条对应的主键),利用GET 请求设置路由        ?nid={{ 数据.id }}
    11 <a href="/project01/del_classes/?nid={{ class.id }}">删除</a>
    12 5:提交过程中,action路由可以带参数到后端传递数据:
    13 <form action="/project01/Updata_classes/?nid={{ id }}" method="post">
    View Code
  • 相关阅读:
    guzzle 中间件原理
    K8S-K8S 环境搭建
    K8S-k8s 理念知识
    云计算的概念
    Linux-DHCP 交互的过程
    linux-怎么踢出系统当前已连接的用户
    linux-Centos 搭建http yum源
    linux-硬链接与软连接
    linux-centos网络配置bond
    linux-dd 一个测试文件
  • 原文地址:https://www.cnblogs.com/wanghong1994/p/11595743.html
Copyright © 2011-2022 走看看