在django中訪問靜態文件(js css img)


剛開始參考的是別的文章,后來參考文章《各種 django 靜態文件的配置總結》才看到原來沒有但是沒有注意到版本,折騰了一晚上,浪費了很多很多時間.后來終於知道搜索django1.7訪問靜態文件.真是傻×.

環境:
python 2.7.3
django 1.7.5

django是不善於處理靜態文件這種事情的.這樣的工作要交給nginx或者apache這樣的服務器.但是在調試時還是要配置一下的
django 1.7.5配置訪問靜態文件貌似比其他的版本都要簡單一些.只需要如下步驟:

  1. 收集靜態文件,然后放在app下的static中,目錄如下:
    
    iot_app
    ├── admin.py
    ├── admin.pyc
    ├── forms.py
    ├── forms.pyc
    ├── __init__.py
    ├── __init__.pyc
    ├── migrations
    │   ├── 0001_initial.py
    │   ├── 0001_initial.pyc
    │   ├── 0002_auto_20150317_1623.py
    │   ├── 0002_auto_20150317_1623.pyc
    │   ├── __init__.py
    │   └── __init__.pyc
    ├── models.py
    ├── models.pyc
    ├── static
    │   ├── css
    │   │   ├── bootstrap.css
    │   │   ├── bootstrap.css.map
    │   │   ├── bootstrap.min.css
    │   │   ├── bootstrap-theme.css
    │   │   ├── bootstrap-theme.css.map
    │   │   └── bootstrap-theme.min.css
    │   ├── fonts
    │   │   ├── glyphicons-halflings-regular.eot
    │   │   ├── glyphicons-halflings-regular.svg
    │   │   ├── glyphicons-halflings-regular.ttf
    │   │   ├── glyphicons-halflings-regular.woff
    │   │   └── glyphicons-halflings-regular.woff2
    │   ├── images
    │   │   ├── chrome.png
    │   │   ├── firefox.png
    │   │   ├── ie.png
    │   │   ├── opera.png
    │   │   └── safari.png
    │   └── js
    │       ├── bootstrap.js
    │       ├── bootstrap.min.js
    │       ├── html5shiv.js
    │       ├── html5shiv.min.js
    │       ├── jquery-1.11.1.js
    │       ├── jquery-1.11.1.min.js
    │       ├── jquery-1.11.1.min.map
    │       ├── jquery-1.11.2.min.js
    │       ├── jquery-1.11.2.min.map
    │       ├── npm.js
    │       └── respond.min.js
    ├── templates
    │   ├── base.html
    │   ├── buttons.html
    │   ├── contact.html
    │   ├── form.html
    │   ├── form_inline.html
    │   ├── formset.html
    │   ├── form_using_template.html
    │   ├── index.html
    │   ├── login.html
    │   ├── pagination.html
    │   └── tabs.html
    ├── tests.py
    ├── views.py
    └── views.pyc
    
  2. 設置setting.py
    STATIC_URL = '/static/'
    STATICFILES_DIRS = (
        os.path.join(BASE_DIR, 'static/'),
    #    '/var/www/static',
    )
  3. 在模版中使用
    {% load staticfiles %} < img src = "{% static "img/test.jpg" %}" alt = "test"/>
    或者
    {% load staticfiles %} < link rel="stylesheet" href="{% static 'css/header.css' %}" >

注意:

  • 確保setting.py中的INSTALL_APPS中包含django.contrib.staticfiles
  • 確保setting.py中的DEBUG選項為True,否則無法映射到靜態文件目錄

在生產模式中不可設置為True哦,所以生產模式下的配置是不一樣的,下面使用nginx來配置靜態資源.
補充:
說明一下,有關幾個settings中設置的問題 static_root static_url staticfiles_dirs:

  1. settings.py中
    As for the static folder under myproject root, actually it doesn’t necessarily have to be there. As long as STATIC_ROOT (in settings.py) points to the same location. So what’s going on here is when you do:

     

    python manage.py collectstatic

    It will copy all the individual static folder to the STATIC_ROOT folder, that’s why in each of your app, you need to follow the naming convention like this:

    app_one/static/app_one/css

    You are not supposed to put stuff directly under the app’s static folder, which might cause name collisions when you collectstatic.
    也就是說.在執行collectstatic時,會自動的把每個app下的static文件拷貝到static_root指定的路徑下.另外采用這樣的目錄格式app_one/static/app_one,這樣在有多個app的情況下.可以就不會因為有文件名相同的文件被覆蓋掉

  2. the STATIC_URL in the settings.py
    This url will appear in your browser:
    (BTW, make sure the URL ends with a slash/, without a slash it might work but very unstable, django will error out this in the log)

     

    
    # STATIC_URL = '/static/' # it doesn't have to this
    STATIC_URL = '/static/monkeyking/' # it can be anything

    You shouldn’t hardcode the img or css in your template something like:

    ... url = "/static/app_one/css/mycss.css" ... 
    ... url = "../../static/app_one/img/logo.png" ... 

    In stead, you should do this in your template:

    {% load staticfiles %}
    < link rel="stylesheet" href="{% static "gl_pulltabs/css/foundation.css" %}" / >

    這個STATIC_URL的作用其實是:在settings.py中DEBUG和TEMPLATE_DEBUG都為開的情況下.只要在模版中像上面這樣使用,就會自動的在前面加上/static/.例如你想要請求的是/static/app_one/css/mycss.css,那么如果你講STATIC_URL設置為/static/appone/,那么你只要使用{% static “css/mycss.css” %}就可以了.

    在開發階段,Django把/static映射到django.contrib.staticfiles這個App。staticfiles自動地從STATICFILES_DIRS、STATIC_ROOT以及各個App的static子目錄里面搜索靜態文件。一旦布署到開發環境上,settings.py不需要重新編寫,只要在Apache的配置文件里面寫好映射,/static將會被Apache處理。django.contrib.staticfiles雖然仍然存在,但因為不會接收到以/static/開始的路徑,所以將不會產生作用。不必擔心Django會使用處理速度變慢。另外,當settings.DEBUG is False的時候,staticfiles將自動關閉

  3. STATICFILES_DIRS
    Here, in settings.py again, you need to explicitly tell django where else to look for static files:

     

    STATICFILES_DIRS = (
    "/path/to/your/project/yourapp/static/",
    ...
    )

    Can you put your deploy static folder(STATIC_ROOT) path to here, so you can save some disk space? No, you cannot! django won’t allow it.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM