使用Djang将静态文件导入模板

2024-03-28 12:33:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我无法将CSS文件加载到HTML模板中。下面是我正在处理的相关文件。有人知道为什么CSS不能加载吗?在

设置.py

BASE_DIR = os.path.dirname(os.path.abspath(__file__))

STATIC_ROOT = 'staticfiles'

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
) 

单词.html

^{pr2}$

网址.py

urlpatterns = patterns('',
    url(r'^
    url(r'^Words', hello.views.index, name='index'),
    url(r'^db', hello.views.db, name='db'),
    url(r'^Add', hello.views.create, name='create'),
    url(r'^admin/', include(admin.site.urls)),
)

urlpatterns += staticfiles_urlpatterns()

单词.css

 .pri {
     color: blue;
 }, hello.views.index, name='index'),

项目结构

murmurwall/
├── Procfile
├── README.md
├── hello
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── __pycache__
│   │   ├── __init__.cpython-34.pyc
│   │   ├── admin.cpython-34.pyc
│   │   ├── forms.cpython-34.pyc
│   │   ├── models.cpython-34.pyc
│   │   └── views.cpython-34.pyc
│   ├── admin.py
│   ├── admin.pyc
│   ├── forms.py
│   ├── forms.pyc
│   ├── management
│   │   └── commands
│   │       ├── CSV
│   │       │   └── Pretty\ Little\ Liars.csv
│   │       ├── __pycache__
│   │       │   └── update_words.cpython-34.pyc
│   │       └── update_words.py
│   ├── models.py
│   ├── models.pyc
│   ├── static
│   │   └── hello
│   │       └── words.css
│   ├── templates
│   │   ├── add_word.html
│   │   ├── base.html
│   │   ├── db.html
│   │   └── words.html
│   ├── tests.py
│   ├── views.py
│   └── views.pyc
├── manage.py
├── murmurwall
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── __pycache__
│   │   ├── __init__.cpython-34.pyc
│   │   ├── settings.cpython-34.pyc
│   │   ├── urls.cpython-34.pyc
│   │   └── wsgi.cpython-34.pyc
│   ├── settings.py
│   ├── settings.pyc
│   ├── static
│   │   └── humans.txt
│   ├── urls.py
│   ├── urls.pyc
│   ├── wsgi.py
│   └── wsgi.pyc
├── requirements.txt
├── runtime.txt
└── staticfiles
    ├── admin
    │   ├── css
    │   │   ├── base.css
    │   │   ├── changelists.css
    │   │   ├── dashboard.css
    │   │   ├── forms.css
    │   │   ├── ie.css
    │   │   ├── login.css
    │   │   ├── rtl.css
    │   │   └── widgets.css
    │   ├── img
    │   │   ├── changelist-bg.gif
    │   │   ├── changelist-bg_rtl.gif
    │   │   ├── default-bg-reverse.gif
    │   │   ├── default-bg.gif
    │   │   ├── deleted-overlay.gif
    │   │   ├── gis
    │   │   │   ├── move_vertex_off.png
    │   │   │   └── move_vertex_on.png
    │   │   ├── icon-no.gif
    │   │   ├── icon-unknown.gif
    │   │   ├── icon-yes.gif
    │   │   ├── icon_addlink.gif
    │   │   ├── icon_alert.gif
    │   │   ├── icon_calendar.gif
    │   │   ├── icon_changelink.gif
    │   │   ├── icon_clock.gif
    │   │   ├── icon_deletelink.gif
    │   │   ├── icon_error.gif
    │   │   ├── icon_searchbox.png
    │   │   ├── icon_success.gif
    │   │   ├── inline-delete-8bit.png
    │   │   ├── inline-delete.png
    │   │   ├── inline-restore-8bit.png
    │   │   ├── inline-restore.png
    │   │   ├── inline-splitter-bg.gif
    │   │   ├── nav-bg-grabber.gif
    │   │   ├── nav-bg-reverse.gif
    │   │   ├── nav-bg-selected.gif
    │   │   ├── nav-bg.gif
    │   │   ├── selector-icons.gif
    │   │   ├── selector-search.gif
    │   │   ├── sorting-icons.gif
    │   │   ├── tooltag-add.png
    │   │   └── tooltag-arrowright.png
    │   └── js
    │       ├── LICENSE-JQUERY.txt
    │       ├── SelectBox.js
    │       ├── SelectFilter2.js
    │       ├── actions.js
    │       ├── actions.min.js
    │       ├── admin
    │       │   ├── DateTimeShortcuts.js
    │       │   └── RelatedObjectLookups.js
    │       ├── calendar.js
    │       ├── collapse.js
    │       ├── collapse.min.js
    │       ├── core.js
    │       ├── inlines.js
    │       ├── inlines.min.js
    │       ├── jquery.init.js
    │       ├── jquery.js
    │       ├── jquery.min.js
    │       ├── prepopulate.js
    │       ├── prepopulate.min.js
    │       ├── timeparse.js
    │       └── urlify.js
    ├── hello
    │   └── words.css
    └── humans.txt

Tags: pyurlhelloadminpnginithtmljs
1条回答
网友
1楼 · 发布于 2024-03-28 12:33:17

您的STATIC_ROOT = 'staticfiles'必须是静态文件的绝对路径。例如STATIC_ROOT = os.path.join(PROJECT_ROOT, STATIC_URL.strip("/"))。这可能需要更改才能进行部署。我在openshift中使用的一个示例: if 'OPENSHIFT_REPO_DIR' in os.environ: STATIC_ROOT = os.path.join(os.environ.get('OPENSHIFT_REPO_DIR'), 'wsgi', 'static') else: STATIC_ROOT = os.path.join(PROJECT_ROOT, STATIC_URL.strip("/"))

另请参见django static static url static root

相关问题 更多 >