-4

好的

所以你不喜欢我的最后一个问题

这是第二个问题,提供的信息比实际需要的多得多。在 flask-bootstrap 模块中定义基本目录是一个非常简单的问题。如果您设法看到我的最后一个问题,我展示了我的代码,但这里有一些您想要的更多无用信息,原因很奇怪:

索引.html

{% extends 'bootstrap/base.html' %}
<!-- TEST THINGS ON A DIFFERENT REPL! -->


<!DOCTYPE html>
<html>
  <head>

    <!-- This is for the tab -->
    <title>Helliott-chip</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <link href="{{ url_for('static', filename='style.css') }}" rel="stylesheet" type="text/css" />

  </head>
  <body>
    <!-- Title on top -->
    <h1><center>Helliot-Chip</center></h1>
    <p>New User? <a href="{{ url_for('register') }}">Click to Register!</a></p>

    <!-- Grey Menu Select -->
    <div class="scrollmenu1">
      {% if current_user.is_anonymous %}
      <a href="{{ url_for('login') }}">Login</a>
      {% else %}
      <a href="{{ url_for('logout') }}">Logout</a>
      <a href="{{ url_for('user', username=current_user.username) }}">Profile</a>
      {% endif %}
      <a href="{{ url_for('news') }}">Home</a>
      <a href="{{ url_for('about') }}">About</a>
      <a href="{{ url_for('explore') }}">Explore</a>
      <a href="{{ url_for('videos') }}">Videos</a>
      <a href="{{ url_for('music') }}">Music</a>
    </div>

    {% block content %}
          <h1>Hi, {{ current_user.username }}!</h1>
    {% endblock %}

    {% block app_content %}{% endblock %}

    {% block scripts %}
        {{ super() }}
        {{ moment.include_moment() }}
    {% endblock %}
  </body>
</html>

初始化.py

import logging
from logging.handlers import SMTPHandler, RotatingFileHandler
import os
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_login import LoginManager
from config import Config
from flask_mail import Mail
from flask_bootstrap import Bootstrap
from flask_moment import Moment

app = Flask(__name__)
Bootstrap(app)
app.config.from_object(Config)
db = SQLAlchemy(app)
migrate = Migrate(app, db)
login = LoginManager(app)
login.login_view = 'login'
mail = Mail(app)
moment = Moment(app)

from app import models, errors

所以现在我把它搞定了,我的错误(在我将 index.html 中的索引更改为 base 之后):

[2021-04-13 17:18:39,365] INFO in debughelpers: Locating template "index.html":
    1: trying loader of application "app"
       class: jinja2.loaders.FileSystemLoader
       encoding: 'utf-8'
       followlinks: False
       searchpath:
         - /home/runner/Website30/app/templates
       -> found ('/home/runner/Website30/app/templates/index.html')
    2: trying loader of blueprint "bootstrap" (flask_bootstrap)
       class: jinja2.loaders.FileSystemLoader
       encoding: 'utf-8'
       followlinks: False
       searchpath:
         - /opt/virtualenvs/python3/lib/python3.8/site-packages/flask_bootstrap/templates
       -> no match
[2021-04-13 17:18:39,372] INFO in debughelpers: Locating template "bootstrap/base.html":
    1: trying loader of application "app"
       class: jinja2.loaders.FileSystemLoader
       encoding: 'utf-8'
       followlinks: False
       searchpath:
         - /home/runner/Website30/app/templates
       -> no match
    2: trying loader of blueprint "bootstrap" (flask_bootstrap)
       class: jinja2.loaders.FileSystemLoader
       encoding: 'utf-8'
       followlinks: False
       searchpath:
         - /opt/virtualenvs/python3/lib/python3.8/site-packages/flask_bootstrap/templates
       -> found ('/opt/virtualenvs/python3/lib/python3.8/site-packages/flask_bootstrap/templates/bootstrap/base.html')
172.18.0.1 - - [13/Apr/2021 17:18:39] "GET / HTTP/1.1" 200 -
[2021-04-13 17:18:40,173] INFO in debughelpers: Locating template "404.html":
    1: trying loader of application "app"
       class: jinja2.loaders.FileSystemLoader
       encoding: 'utf-8'
       followlinks: False
       searchpath:
         - /home/runner/Website30/app/templates
       -> found ('/home/runner/Website30/app/templates/404.html')
    2: trying loader of blueprint "bootstrap" (flask_bootstrap)
       class: jinja2.loaders.FileSystemLoader
       encoding: 'utf-8'
       followlinks: False
       searchpath:
         - /opt/virtualenvs/python3/lib/python3.8/site-packages/flask_bootstrap/templates
       -> no match

在我改变它之前我的错误:

jinja2.exceptions.TemplateNotFound: bootstrap/index.html

现在这一次,我的问题一般不会被任何答案关闭(我可能会注意到这与本网站的目的相反)这里有什么问题?我初始化flask-bootstrap的方式有问题,还是我需要在终端中输入一些命令?随时提出问题,如果这是我遗漏的一个简单问题,非常感谢。对于任何不必要的态度,我深表歉意,我只是对因“信息不足”而锁定我最后一个问题的人感到失望。

4

1 回答 1

0

您的初始化方式存在问题flask-bootstrap。你应该这样做:

# Your previous imports
from flask_bootstrap import Bootstrap

app = Flask(__name__)

bootstrap = Bootstrap(app)

# ...

基本上,更新这一行:

Bootstrap(app)

至:

bootstrap = Bootstrap(app)

这正是您为其他已安装的软件包所做的。

维护{% extends 'bootstrap/base.html' %}基本模板(即父模板)中的行。不要将其更改为{% extends 'bootstrap/index.html' %}此文件从引导程序的基本模板继承样式、布局、功能等。

<!-- base.html -->




{% extends 'bootstrap/base.html' %}

{% block head %}
    <!-- Head information goes here -->
{% endblock %}

{% block navbar %}
    <!-- Your navbar goes here -->
{% endblock %}

{% block content %}
<!-- flash message goes here -->

    {% block app_content %}
        <!-- Content from your custom HTML templates will go here -->
    {% endblock %}

{% endblock %}

{% block scripts %}
    <!-- Your scripts go here -->
{% endblock %}

index.html继承您的文件中base,html,您将执行以下操作:

<!-- index.html -->


{% extends 'base.html' %}

{% block app_content %}
    <!-- Content goes here -->
{% endblock %}
于 2021-04-14T03:29:33.147 回答