Django和引导:脱机重新加载页面问题(netdna.bootstrapcdn.com)

Django和引导:脱机重新加载页面问题(netdna.bootstrapcdn.com),django,twitter-bootstrap-3,offlineapps,Django,Twitter Bootstrap 3,Offlineapps,我有一个Django项目,有效。我在我的基本html中使用了一些“引导”功能: 在设置.py中: INSTALLED_APPS = ( 'bootstrap_toolkit', 'bootstrap3', 'django_admin_bootstrapped.bootstrap3', 'django_admin_bootstrapped', 'django.contrib.admin', 'django.contrib.a

我有一个Django项目,有效。我在我的基本html中使用了一些“引导”功能:

设置.py中

INSTALLED_APPS = (
    'bootstrap_toolkit',
    'bootstrap3',
    'django_admin_bootstrapped.bootstrap3',
    'django_admin_bootstrapped',             
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'jquery',
    'jquery_ui',
    'homepage',
    'simpleapp',
)
完美的,如果我使用我的按钮和公司在页面之间冲浪。它看起来是引导的,一切都很好

但是…

我注意到,如果我的电脑处于脱机状态,(只有)当我重新加载页面时(或者我在一些js parent.window.location.reload(true)中使用),它似乎没有启动!此外,我注意到当我在线并重新加载页面时,浏览器会搜索netdna.bootstrapcdn.com。。。但这不是我的目标:我必须创建一个离线工作的项目


如何解决重新加载问题?

如果浏览器上没有启用缓存,它将不会存储从CDN下载的CSS和JS。为了避免这种情况

  • 在浏览器中启用缓存
  • 下载缩小的引导css和js,并将它们放在项目的静态目录中。然后像
    一样链接到它们。如果你走这条路,别忘了发布你的静态文件

  • django-bootstrap3的默认设置指向CDN,如下所示:

    您应该将相关文件存储在
    static
    目录中,并设置
    bootstrap3
    jquery\u url
    base\u url
    设置。例如:

    BOOTSTRAP3 = {
    
        # The URL to the jQuery JavaScript file
        'jquery_url': '/static/js/jquery.min.js',
    
        # The Bootstrap base URL
        'base_url': '/static/css/',
    
        # The complete URL to the Bootstrap CSS file (None means derive it from base_url)
        'css_url': '/static/css/bootstrap.min.css',
    
        # The complete URL to the Bootstrap CSS file (None means no theme)
        'theme_url': '/static/css/bootstrap.theme.min.css',
    
        # The complete URL to the Bootstrap JavaScript file (None means derive it from base_url)
        'javascript_url': '/static/js/bootstrap.min.js',
    
    另一种方法是将
    css
    js
    位置硬编码到模板中,如:

    <link href="{% static 'css/bootstrap.min.css' %}" rel="stylesheet" type="text/css"/>
    <link href="{% static 'css/bootstrap.theme.min.css' %}" rel="stylesheet" type="text/css"/>
    <script src="{% static 'js/bootstrap.min.js' %}"></script>
    
    
    
    您可能应该使用Bootstrap 3应用程序的设置,将其指向正确的JS和CSS位置,以避免两次加载引导(一次从CDN加载,一次在本地加载)。此外,启用缓存并不是使网站离线运行的正确方法。如果您的资产托管在CDN上,而您不缓存它们,则除非您在本地有可用的资产,否则无法使其离线。可以在
    settings.py
    中轻松定义一些内容,如
    OFFLINE\u DEV=True
    以选择仅在脱机时加载本地版本。实际上,如果未缓存本地版本,则在开发时无法将其脱机。然而,即使你缓存它们,如果你强制刷新并破坏缓存,它们也不会离线(例如,如果你重新加载,Chrome也会这样做)。缓存不是离线访问的正确方式。托管本地版本是(值得称赞的是,这是你的第二个建议)。
    BOOTSTRAP3 = {
    
        # The URL to the jQuery JavaScript file
        'jquery_url': '/static/js/jquery.min.js',
    
        # The Bootstrap base URL
        'base_url': '/static/css/',
    
        # The complete URL to the Bootstrap CSS file (None means derive it from base_url)
        'css_url': '/static/css/bootstrap.min.css',
    
        # The complete URL to the Bootstrap CSS file (None means no theme)
        'theme_url': '/static/css/bootstrap.theme.min.css',
    
        # The complete URL to the Bootstrap JavaScript file (None means derive it from base_url)
        'javascript_url': '/static/js/bootstrap.min.js',
    
    <link href="{% static 'css/bootstrap.min.css' %}" rel="stylesheet" type="text/css"/>
    <link href="{% static 'css/bootstrap.theme.min.css' %}" rel="stylesheet" type="text/css"/>
    <script src="{% static 'js/bootstrap.min.js' %}"></script>