• [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文件夹 

       


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

  • 相关阅读:
    c# 集合的交集、并集、差集
    git版本控制
    jquery html动态添加的元素绑定事件详解
    Binding笔记
    动画
    MSSQL 索引
    TCP和UDP的优缺点及区别
    Fetch API 了解 及对比ajax、axois
    提供图片服务网站
    2017 jq 总结
  • 原文地址:https://www.cnblogs.com/AngryZe/p/9020359.html
Copyright © 2020-2023  润新知