• django项目创建和启动,静态资源配置 django模板遍历数组和对象 if forloop


    工程目录下,cmd:django-admin startproject 工程名(djdemo1),创建django工程。此时工程下会生动djdemo1主模块。
    cmd:django-admin startapp(app1) 模块名,创建新的子模块app1。

    创建模块后(假设模块名app1),在/djdemo1\settings.py的INSTALLED_APPS加上’app1’,
    django就会在需要时扫描app1里的templates文件夹,查找需要的html文件。
    如果不加的话,需要在TEMPLATES.DIRS里加上app1的templates的路径。比如:
    TEMPLATES = [{‘DIRS’: [***
    # os.path.join(BASE_DIR, “/app1/templates/”),
    ], ***}

    cmd:python 工程目录/manage.py runserver localhost:8000开启服务器在8000端口。

    引入静态资源:
    在子模块下创建static/css/a.js,
    在view里返回的html文件顶部加上{% load static %}(否则报错{% load static %}),
    head里加上,即可引用该js。
    文件放在其他子模块里也可访问,放在主模块里无法访问。

    本文示例代码如下

    //app1\views.py
    from django.shortcuts import render
    from django.http import HttpResponse
    
    
    def hello(request):
        return HttpResponse("Hello world ! ")
    
    # render , set parameters for response, django template
    def t1(request):
        user={'name':'John','age':11,'password':'aaa'}
        products=[{'product':'苹果{}'.format(i),'price':i*10+1} for i in range(5)]
        return render(request, "t1.html",{'from':__file__,'user':user,'products':products})
    
    # set static/css or js for html files
    def t2(request):
        return render(request, "t2.html")
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    //app1\templates\t1.html
    {%load static%}
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <script src="{% static 'js/111.js' %}"  ></script>
    </head>
    <body>
        t1 html from {{from}}<br>
        <div>对象取属性:{{user.password}}</div>
        <div>products[0]price的写法是:{{products.0.price}}</div>
        <h3>遍历对象:</h3>
        {%for k,v in user.items%}
            <div>{{k}}>>{{v}}</div>
        {%endfor%}
        <h3>遍历数组:</h3>
        {%for p in products%}
            <div> {{forloop}}>>{{p}}</div>
        {%endfor%}
        <h3>if else</h3>
        {%if 3 > 4 %}
            <span>case if1</span>
        {%elif 41 > 5 %}
            <span>case elif</span>
        {%else%}
            <span>case else</span>
        {%endif%}
    </body>
    </html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    from django.contrib import admin
    from django.urls import path
    import app1.views as a1v
    import app2.views as a2v
    
    urlpatterns = [
        path('admin/', admin.site.urls),
    
        path('a1', a1v.hello),
        path('a1/t1', a1v.t1),
        path('a1/t2', a1v.t2),
    
        path('a2/t1', a2v.t1),
    
    ]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    t1.html输出如下:
    
    t1 html from D:\workspace\python_vscode\djdemo1\app1\views.py
    对象取属性:aaa
    products[0]price的写法是:1
    遍历对象:
    name>>John
    age>>11
    password>>aaa
    遍历数组:
    {'parentloop': {}, 'counter0': 0, 'counter': 1, 'revcounter': 5, 'revcounter0': 4, 'first': True, 'last': False}>>{'product': '苹果0', 'price': 1}
    {'parentloop': {}, 'counter0': 1, 'counter': 2, 'revcounter': 4, 'revcounter0': 3, 'first': False, 'last': False}>>{'product': '苹果1', 'price': 11}
    {'parentloop': {}, 'counter0': 2, 'counter': 3, 'revcounter': 3, 'revcounter0': 2, 'first': False, 'last': False}>>{'product': '苹果2', 'price': 21}
    {'parentloop': {}, 'counter0': 3, 'counter': 4, 'revcounter': 2, 'revcounter0': 1, 'first': False, 'last': False}>>{'product': '苹果3', 'price': 31}
    {'parentloop': {}, 'counter0': 4, 'counter': 5, 'revcounter': 1, 'revcounter0': 0, 'first': False, 'last': True}>>{'product': '苹果4', 'price': 41}
    
    if elsecase elif
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    其他注意点

    django模板遍历数组时可以从{{forloop.counter0}}对象获取index
    django模板里变量和运算符号间要加空格,否则报错

  • 相关阅读:
    Opengl ES之踩坑记
    基于Socket编程下 实现Linux-Linux、Linux-Windows tcp通信
    d435i 相机和imu标定
    一文1700字使用Postman搞定各种接口token实战(建议收藏)
    如何建设水利数字孪生流域
    数据分析-Pandas数据探查初步:离散点图
    VisionPro学习笔记(6)——如何使用QuickBuild
    springboot+人力资源管理系统 毕业设计-附源码181614
    可下载《2022年中国数字化办公市场研究报告》详解1768亿元市场
    富文本文案存储翻译方案
  • 原文地址:https://blog.csdn.net/weixin_43292547/article/details/126438335