飞天使-template模版相关知识

遇到报错django.template.exceptions.TemplateSyntaxError: 'staticfiles' is not a registered tag library. Must

复制代码
ROOT_URLCONF = ''

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')]
        ,
        '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',
                
            ],
            #添加下面内容
            'libraries': { # Adding this section should work around the issue.
                'staticfiles' : 'django.templatetags.static',
            },
        },
    },
]
解决办法参考链接:https://www.cnblogs.com/yizhipanghu/p/15346297.html

html 模版中新增一个图片

复制代码
    <div id="title">
        <div><img src="{% static "imgs/stu04.jpg" %}"></div>
        <div>学生信息</div>
    </div>
#STATICFILES_DIRS --- 全局变量定义了存储静态文件集合
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),
    os.path.join(BASE_DIR, 'abc'),
    os.path.join(BASE_DIR, 'app01', 'static')
]

url 跳转-A标签(链接)

复制代码
        <div>
             <div><a href="/"><img src="{% static "yk-logo-1220.png" %}"></a></div>
             <div id="detail">综艺首页</div>
        </div>
        ```
 
 ####  redirect 关键字处理跳转
 用户没有登陆直接跳转到登陆页面
 from django.shortcuts import render
from django.shortcuts import redirect

# Create your views here.


def index(request): # 首页
    # url记录登录名 --- ? username=alice
    username = request.GET.get("username")
    # 如果获取到username值,直接显示首页,获取不到;调到登录页

    if username:
        return render(request, 'index.html')
    else:
        # 跳转到登录页
        return redirect("/login/")

多app下的templates 环境准备

复制代码
setting 里面设置
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'home',
    'tv',
    'movie',
]

多app下模块的应用

复制代码
每个app 会有一个index.html ,多个app会出现重复情况
则每个app下面可以建立一个相同app名字,比如tv ,tv/index.html ,xx ,xx/index.html
相关推荐
运维行者_23 分钟前
ISP 企业级带宽计费怎么做?网络流量计费的 6 大核心能力
运维·服务器·网络·数据库·支持向量机·接口隔离原则
奇树谦28 分钟前
PCB生产制造全流程详解:从开料、钻孔、沉铜到检测、包装
网络·数据库·制造
驾驭人生1 小时前
Hangfire.Redis.StackExchange 已归档停更!生产任务卡死、重复执行、网络抖动问题解决方案
数据库·redis·缓存
xierui1231231 小时前
Agent记忆不是聊天记录:用三层存储构建可追溯的 AI 工作流
数据库·人工智能·aigc·软件工程
2301_800074211 小时前
mysql DQL
数据库·sql·mysql
西风未眠1 小时前
ClickHouse数据库引擎引起的数据丢失
数据库·clickhouse·故障
海浪仙人掌1 小时前
应收账款分析如何操作?应收账款分析有哪些注意事项?
数据库·人工智能
Elastic 中国社区官方博客1 小时前
在 Elasticsearch 中回填时间序列数据:通过批量 API 加载数月的历史指标数据
大数据·运维·数据库·人工智能·elasticsearch·搜索引擎·全文检索
Horn Still Sounds2 小时前
Linux网络并发服务器模型与SQLite数据库学习笔记
linux·服务器·数据库
昵称画2 小时前
POC验证怎么设计用例?不走过程的实操要点
大数据·数据库·人工智能·低代码·excel