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

       


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

  • 相关阅读:
    简单构建一个xmlhttp对象池合理创建和使用xmlhttp对象
    iBATIS.net获取运行时sql语句
    不做自了汉,大家好才是真的好
    sql查询,nolock写还是不写,这是一个问题
    Sublime Text 2 快捷键用法大全(转)
    javascript设计模式入门之策略模式
    记一次外单前端页面编写小结
    代码驾驭
    一次项目总结,内容设置页面
    【百度地图API】今日小年大进步,齐头共进贺佳节——API优化升级上线,不再增加内存消耗
  • 原文地址:https://www.cnblogs.com/AngryZe/p/9020359.html
Copyright © 2011-2022 走看看