簡體   English   中英

無法訪問CSS文件

[英]Can't access CSS -files

有一些問題來訪問我的CSS腳本。 設置:

STATIC_URL = '/static/'

# Additional locations of static files
STATICFILES_DIRS = (
    '/C:/NetMagProjekt/netmag/netmag/static',
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
)

模板庫

  <link rel="stylesheet" type="text/css" href="{{STATIC_URL}}style.css"/>

如果我在瀏覽器中查看源代碼,它看起來像這樣:

 <link rel="stylesheet" type="text/css" href="{{STATIC_URL}}style.css"/>

我找不到css風格。

有任何想法嗎?

  1. C:之前刪除前導后擋板C:

  2. <link rel="stylesheet" type="text/css" href="{{STATIC_URL}}style.css"/>更改為<link rel="stylesheet" type="text/css" href="/static/style.css"/>

如果這些沒有幫助,那么嘗試:

  1. 確保settings.py包含以下內容:
STATICFILES_FINDERS = (
  'django.contrib.staticfiles.finders.FileSystemFinder',
  'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
  1. 確保urls.py包含以下內容:
# redirects to static media files (css, javascript, images, etc.)
(r'^static/(?P<path>.*)$', 
   'django.views.static.serve', {'document_root': 'static/'}),

首先,你必須修復你的代碼:

STATICFILES_DIRS = (
    'C:/NetMagProjekt/netmag/netmag/static',
)

還嘗試了一件事,而不是使用靜態dir選項,

STATIC_ROOT = 'C:/NetMagProjekt/netmag/netmag/static'

嘗試兩種選擇。

嘗試collectstatic django命令。

例如 :

cd project_dir
python manage.py collectstatic

有關詳細信息,請參閱此處

把這行放在root urls.py文件中。

# redirects to static media files (css, javascript, images, etc.)
(r'^static/(?P<path>.*)$', 
   'django.views.static.serve', {'document_root': 'static/'}),

我會以編程方式獲取基本路徑以避免問題。

這是我在項目中設置的方式。

import os

BASE = os.path.abspath(os.path.dirname(__name__))

STATICFILES_DIRS = (
    os.path.join(BASE, "<your dir name>"),
)

我建議你使用python console (在終端中使用python命令啟動(可能,我不在Windows上),然后運行並打印命令以查看。

例:

$>python
>>> BASE = os.path.abspath(os.path.dirname(__name__))
>>> BASE
'/string/to/directory/I/am/in/currently'

這樣就會返回設置文件的絕對路徑。 然后你只需要加入那個靜態文件所在的路徑就可以了。

有幾件事可能會導致它。

是否定義了STATIC_ROOT?

STATIC_ROOT = 'C:/NetMagProjekt/netmag/netmag/static'

當然,確定路徑是正確的,並在c之前擺脫額外的反斜杠:

在您的模板中,請確保您擁有

{% load staticfiles %}

即使擴展加載靜態文件的模板,我相信你需要再次添加它。 然后可能對於src url,這可能會有所幫助:

<link rel="stylesheet" type="text/css" href="{% static "style.css" %}">

最后,加載您的靜態文件:

python manage.py collectstatic

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM