zoukankan      html  css  js  c++  java
  • Django 模版语法 一

    创建项目 django_template 和 app

    django-admin startproject django_template
    python manage.py startapp app01
    

    在 settings.py 中修改

    INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'app01',
    ]
    
    ...
    
     'DIRS': [os.path.join(BASE_DIR, 'templates')],
    

    在 urls.py 中修改:

    from django.contrib import admin
    from django.urls import path
    from app01 import views
    
    urlpatterns = [
        path('admin/', admin.site.urls),
        path('index/', views.index),
    ]
    

    在 views.py 中添加函数

    def index(req):
        s="hello"
        s2=[1,22,333]
        return render(req,"index.html",{"list":s2})
    
    

    创建 templates 文件夹,添加 index.html 文件,获取第三个数组的值

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <h1>Template</h1>
    {{ list.2 }}
    </body>
    </html>
    
    python manage.py runserver 8000
    

    访问 http://127.0.0.1:8000/index/

    传递字典

    在 views.py 中添加

    def index(req):
        s="hello"
        s2=[1,22,333]
        s3={"username":"klvchen", "age":30}
        return render(req,"index.html",{"obj":s3})
    
    

    在 index.html 中修改

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <h1>Template</h1>
    {{ obj }}
    {{ obj.username }}
    </body>
    </html>
    

    传递时间

    在 views.py 中添加

    from django.shortcuts import render
    import datetime
    
    # Create your views here.
    
    def index(req):
        s="hello"
        s2=[1,22,333]
        s3={"username":"klvchen", "age":30}
        s4=datetime.datetime.now()
        return render(req,"index.html",{"obj":s4})
    

    在 index.html 中修改

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <h1>Template</h1>
    {{ obj.year }}
    {{ obj.month }}
    </body>
    </html>
    

    传递类

    在 views.py 中添加

    import datetime
    
    # Create your views here.
    
    def index(req):
    
        class Person:
            def __init__(self, name, age):
                self.name = name
                self.age = age
    
        s="hello"
        s2=[1,22,333]
        s3={"username":"klvchen", "age":30}
        s4=datetime.datetime.now()
        s5=Person("klvchen", 30)
        return render(req,"index.html",{"obj":s5})
    

    在 index.html 中修改

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <h1>Template</h1>
    {{ obj.name }}
    {{ obj.age }}
    {{ obj }}
    </body>
    </html>
    

    {% if %} ,{% for %}的使用

    在 views.py 中修改

    def index(req):
    
        s="hello"
        s2=[1,22,333]
        return render(req,"index.html",{"obj":s2})
    

    在 index.html 中修改

    {% if True %}
        <p>hello world</p>
    {% endif %}
    
    #循环数组
    {% for i in obj %}
        <p>{{ i }}</p>
    {% endfor%}
    
    #显示下标
    {% for i in obj %}
        <p>{{ forloop.counter}}:{{ i }}</p>
        <!-- 下标从0 开始 -->
        <p>{{ forloop.counter0}}:{{ i }}</p>
        <!-- 倒序 -->
        <p>{{ forloop.revcounter}}:{{ i }}</p>
    {% endfor %}
    
  • 相关阅读:
    Winform 自定义TabControl实现浏览器标签
    LeetCode Add Two Numbers
    Java基础知识复习(二)
    Java基础知识复习(一)
    0-1背包问题复习
    centos7 vmware克隆解决网络问题
    阿里云上Docker Compose部署wordpress
    不需要瞎折腾,官方文档才是终极
    centos7 nginx安装
    iptables练习题(四)
  • 原文地址:https://www.cnblogs.com/klvchen/p/10815351.html
Copyright © 2011-2022 走看看