Bootstrap Django中的引导崩溃问题

hlswsv35  于 2022-12-08  发布在  Bootstrap
关注(0)|答案(3)|浏览(162)

我尝试在Django中使用引导页面,不幸的是列表组项的折叠不起作用。
相关部分取自html:

<link rel="stylesheet" href="{% static 'conf/css/bootstrap.css' %}">

      <div class="panel panel-default">
        <div class="panel-heading" role="tab" id="headingTwo">
          <h4 class="panel-title">
            <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
              Header
            </a>
          </h4>
        </div>
        <div id="collapseTwo" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo">
          <div class="list-group">
            <a href="#" class="list-group-item">Item1</a>
            <a href="#" class="list-group-item">Item2</a>
          </div>
        </div>
      </div>

<!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> 
<script src="{% static 'conf/js/jquery-1.11.2.min.js' %}"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="{% static 'conf/js/bootstrap.min.js' %}"></script>

当点击标题时,“#collapseTwo”信号会被发送到浏览器的django应用程序中,而不是折叠列表(例如:127.0.0.1:8000/conf/#collapseTwo)。在django之外,折叠工作正常。在django内,除了折叠之外的其他一切都工作正常(css,链接,图片等)
django项目中的URL模式:

urlpatterns = [
url(r'^$', RedirectView.as_view(url='conf', permanent=False), name='index'),
url(r'^accounts/login/$', 'django.contrib.auth.views.login', {'template_name': 'admin/login.html'}),
url(r'^accounts/logout/$', 'django.contrib.auth.views.logout'),
url(r'^admin/', include(admin.site.urls)),
url(r'^conf/', include('conf.urls')),
]

名为“conf”的应用程序的URL模式:

urlpatterns = [
url(r'^$', views.index, name='index'),
]

主要查看关键部位:

def index(request):

    object = AudioSetting.objects.get(pk=1)
    DefaultText = object.text   
    context = {'DefaultText': DefaultText,}

    template = "conf/index.html"

    return render(request, template, context)

你能告诉我如何在django中使用崩溃吗?
编辑:渲染页面时来自django服务器的消息,包括正在使用的js和css:

System check identified no issues (0 silenced).
October 11, 2015 - 20:51:06
Django version 1.8.5, using settings 'project.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[11/Oct/2015 20:51:14] "GET / HTTP/1.1" 302 0
[11/Oct/2015 20:51:14] "GET /conf HTTP/1.1" 301 0
[11/Oct/2015 20:51:14] "GET /conf/ HTTP/1.1" 200 9041
[11/Oct/2015 20:51:14] "GET /static/conf/css/bootstrap.css HTTP/1.1" 304 0
[11/Oct/2015 20:51:14] "GET /static/conf/img/logo.jpg HTTP/1.1" 304 0
[11/Oct/2015 20:51:15] "GET /static/conf/js/jquery-1.11.2.min.js HTTP/1.1" 304 0
yvfmudvl

yvfmudvl1#

我试过你的html代码w3schools现场编辑器和崩溃工程正常。
因此,在您的情况下,问题可能与其他一些js代码有关,这些代码在单击元素时不会阻止默认事件处理,因为bootstrap本身会这样做:
https://github.com/twbs/bootstrap/blob/master/js/collapse.js#L202
因此,请检查其他js文件和浏览器控制台是否存在任何可能阻止 Bootstrap 处理折叠功能的错误。

iyfamqjs

iyfamqjs2#

最后!答案是,jquery和bootstrap都没有问题,只是Django没有。

只需删除**https://**之前的内容:

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>

之后:

<script src="cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <link href="cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
bt1cpqcv

bt1cpqcv3#

您根本不需要使用锚标记,只需将您的<a>标记替换为<span>

...
<span class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
   Header
</span>
...

结果是完全一样的,但是当用户打开或关闭折叠面板时,可以有效地防止django重定向。为了保持光标的悬停状态,添加以下CSS:

span[role="button"] {
    cursor: pointer;
}

演示-〉http://jsfiddle.net/ae3se1s5/

相关问题