引导轮播不能与烧瓶路由一起使用

时间:2018-12-20 18:32:34

标签: twitter-bootstrap flask

我在index.html中有以下HTML代码:

{% block header %}
<header>
  <div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
    <ol class="carousel-indicators">
      <li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
      <li data-target="#carouselExampleIndicators" data-slide-to="1"></li>
      <li data-target="#carouselExampleIndicators" data-slide-to="2"></li>
    </ol>
    <div class="carousel-inner" role="listbox">
      <!-- Slide One - Set the background image for this slide in the line below -->
      <div class="carousel-item active" style="background-image: url('http://placekitten.com/1900/1080')">
        <div class="carousel-caption d-none d-md-block">
          <h3>First Slide</h3>
          <p>This is a description for the first slide.</p>
        </div>
      </div>
      <!-- Slide Two - Set the background image for this slide in the line below -->
      <div class="carousel-item" style="background-image: url('http://placekitten.com/1900/1080')">
        <div class="carousel-caption d-none d-md-block">
          <h3>Second Slide</h3>
          <p>This is a description for the second slide.</p>
        </div>
      </div>
      <!-- Slide Three - Set the background image for this slide in the line below -->
      <div class="carousel-item" style="background-image: url('http://placekitten.com/1900/1080')">
        <div class="carousel-caption d-none d-md-block">
          <h3>Third Slide</h3>
          <p>This is a description for the third slide.</p>
        </div>
      </div>
    </div>
    <a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
      <span class="carousel-control-prev-icon" aria-hidden="true"></span>
      <span class="sr-only">Previous</span>
    </a>
    <a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next">
      <span class="carousel-control-next-icon" aria-hidden="true"></span>
      <span class="sr-only">Next</span>
    </a>
  </div>
</header>
{% endblock %}

这是我的烧瓶路由器:

@app.route('/')
@app.route('/index')
def index():
    return render_template('index.html')

问题是,当我单击转盘上的下一个按钮时,它将无法工作,并且显示的URL是:

http://127.0.0.1:5000/index#carouselExampleIndicators

我知道问题很明显,但是我不知道解决方案。我试图保持index.html独立..或试图将轮播ID发送回路由器,然后从那里去..但我的所有尝试都是无用的。我使用引导程序4.1.3和烧瓶1.0.2。

2 个答案:

答案 0 :(得分:1)

好像index.html页面缺少指向引导程序的链接。您可以从https://getbootstrap.com/docs/4.3/getting-started/download/获取bootstrap.css和bootstrap.min.js并将css和js文件夹保存在flask模板文件夹(放置index.html的文件夹)中。

<link rel="stylesheet" type="text/css" href="css/bootstrap.css">之后的{% block header %}<script src="js/bootstrap.min.js"></script>之前的{% endblock %}之前添加{% block header %} <link rel="stylesheet" type="text/css" href="css/bootstrap.css"> <header> <div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel"> .... YOUR HTML CODE FOR CAROUSEL GOES HERE .... </div> </header> <script src="js/bootstrap.min.js"></script> {% endblock %} 应该可以使轮播中的图像滑动正常工作

例如:

{{1}}

答案 1 :(得分:0)

以下是在3.1.0版中运行的轮播的完整解决方案:

<div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
    <ol class="carousel-indicators">
      <li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
      <li data-target="#carouselExampleIndicators" data-slide-to="1"></li>
      <li data-target="#carouselExampleIndicators" data-slide-to="2"></li>
    </ol>
    <div class="carousel-inner">
      <!-- Slide One - Set the background image for this slide in the line below -->
      <div class="item active">
      <img src="http://placekitten.com/1900/1080" alt="">
        <div class="carousel-caption d-none d-md-block">
          <h3>First Slide</h3>
          <p>This is a description for the first slide.</p>
        </div>
      </div>
      <!-- Slide Two - Set the background image for this slide in the line below -->
      <div class="item">
      <img src="http://placekitten.com/1900/1080" alt="">
        <div class="carousel-caption d-none d-md-block">
          <h3>Second Slide</h3>
          <p>This is a description for the second slide.</p>
        </div>
      </div>
      <!-- Slide Three - Set the background image for this slide in the line below -->
      <div class="item">
      <img src="http://placekitten.com/1900/1080" alt="">
        <div class="carousel-caption d-none d-md-block">
          <h3>Third Slide</h3>
          <p>This is a description for the third slide.</p>
        </div>
      </div>
    </div>
    <a class="left carousel-control" href="#carouselExampleIndicators" role="button" data-slide="prev">
      <span class="glyphicon glyphicon-chevron-left"></span>
      <span class="sr-only">Previous</span>
    </a>
    <a class="right carousel-control" href="#carouselExampleIndicators" role="button" data-slide="next">
      <span class="glyphicon glyphicon-chevron-right"></span>
      <span class="sr-only">Next</span>
    </a>
  </div>

此外,请确保添加以在烧瓶应用程序中导入并加载扩展程序:

app = Flask(__name__)
Bootstrap(app) # Just add this below above line in your code.