zoukankan      html  css  js  c++  java
  • [Django学习] Django基础(3)_templates与static配置

    Templates配置

    一. templates常规文件配置(文件例子来自https://docs.djangoproject.com/en/2.0/intro/reusable-apps/

    mysite/
        manage.py
        mysite/
            __init__.py
            settings.py
            urls.py
            wsgi.py
        polls/
            __init__.py
            admin.py
            migrations/
                __init__.py
                0001_initial.py
            models.py
            templates/
                polls/
                    detail.html
                    index.html
                    results.html
            tests.py
            urls.py
            views.py
        templates/
            admin/
                base_site.html
    

      

      You created mysite/templates in Tutorial 7, and polls/templates in Tutorial 3. Now perhaps it is clearer why we chose to have separate template directories for the project and application: everything that is part of the polls application is in polls. It makes the application self-contained and easier to drop into a new project.

      The polls directory could now be copied into a new Django project and immediately reused. It’s not quite ready to be published though. For that, we need to package the app to make it easy for others to install.

    二. 配置

      Django有两个内置的backend,分别是Django template language (DTL)和Jinja2。可在setting.py中进行配置。

    TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [os.path.join(BASE_DIR, 'templates'),os.path.join(BASE_DIR, 'templates/Blog'),],
            'APP_DIRS': True,
            'OPTIONS': {
                'context_processors': [
                    'django.template.context_processors.debug',
                    'django.template.context_processors.request',
                    'django.contrib.auth.context_processors.auth',
                    'django.contrib.messages.context_processors.messages',
                ],
            },
        },
    ]
    

      (1)'BACKEND':Django 模版引擎

      (2)'DIRS':一个列表,存放引擎搜索的目录

      (3)'APP_DIRS':是否搜索应用程序内的templates

      (4)'OPTIONS':其他的设置

    三. 常规使用方法

      1. 修改base.html

        使用{% block <参数> %}{% endblock %}来连接用到该base.html结构的app模版的内容

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8" />
            <title>
                {% block title %}{% endblock %}        
            </title>
        </head>
    
        <body>
            {% block content %}
            {% endblock %}
            <br>
            {% block footer %}
            {# 任何每个页面都可能修改的文本区域的页脚 #}
                <p>Thanks for visiting my site! </p>
            {% endblock %}
        </body>
    </html>
    

      

      2. 修改<app>blog_list.html

        (1)文件开头 {% extends "base.html" %} 调用base.html结构

        (2)在{% block <参数> %}与{% endblock %}中间填充需要显示的数据内容

    {% extends "base.html" %}
    
    {% block title %}blog list{% endblock %}
    
    
    {% block content %}
    <div class="content">
        {% for blog in blog_list_html %}
            <h2><a href="{% url 'show_BlogArticle_Detail' blog.id %}">{{ blog.title }}</a></h2>
            <p>{{ blog.content }}</p>
            <p>{{ blog.create_time }}</p>
        {% endfor %}   
    </div>    
    {% endblock %}
    

    Static配置

    一. static常规文件配置

    mysite/
        manage.py
        mysite/
            __init__.py
            settings.py
            urls.py
            wsgi.py
        polls/
            __init__.py
            admin.py
            migrations/
                __init__.py
                0001_initial.py
            models.py
            templates
            static
                polls/
                    images/
                        background.png
                    css/
                        style.css              
            tests.py
            urls.py
            views.py

    二. 使用方法

      在base.html的<head>中加载如下内容

    <head>    
        {% load static %}
        <link rel="stylesheet" type="text/css" href="{% static 'Blog/style.css' %}" />
    </head>
    

      (1)href="{% static 'Blog/style.css' %}"

        为静态文件中css文件的地址,默认查找Project/App/static文件夹 

       


    注明:学习资料来自“再敲一行代码的个人空间”以及“杨仕航的博客”

  • 相关阅读:
    python 包管理工具 pip 的配置
    Python 变量作用域 LEGB (下)—— Enclosing function locals
    Python 变量作用域 LEGB (上)—— Local,Global,Builtin
    2020 Java 面试题 小结 (答案慢慢补上,有错误请指出)
    mysql 根据日期(date)做年,月,日分组统计查询
    jvm指令
    正则表达式 分割地址 获取省市区详细地址
    .Net 异常记录
    WCF设计服务协议(一)
    plsql ORA-01789:查询块具有不正确的结果列数
  • 原文地址:https://www.cnblogs.com/AngryZe/p/9020359.html
Copyright © 2011-2022 走看看