亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 Django 模板中使用 Bootstrap/static 文件

如何在 Django 模板中使用 Bootstrap/static 文件

aluckdog 2023-03-30 10:25:34
好吧,我知道有很多關于這個的問題,我已經梳理了所有這些問題,試圖找到答案,但沒有什么是我正在尋找的。我的問題是,每當我嘗試將靜態文件加載到 Django 模板中時,它就無法工作。這是我的文件結構:MainProjectDir|+-- DjangoProject|  ||  +-- MainDjangoSub|  |  ||  |  +-- __init__.py|  |  +-- settings.py (all the other usual folders below)|  ||  +-- StaticPages (this is my App directory)|  |  ||  |  +-- templates (will contain all templates for StaticPages app)|  |  |  ||  |  |  +-- StaticPages (directory containing html templates for the Static Pages App)|  |  |  |  ||  |  |  |  +-- main.html|  |  |  |  +-- navbar.html|  |  ||  |  +-- __init__.py|  |  +-- views.py (all other usual app files below)|  ||  +-- static (contains all static files for the project)|  |  ||  |  +-- Bootstrap (subdirectory containing the Bootstrap static files)|  |  |  ||  |  |  +-- css (directory containing all css files from Bootstrap precompiled files)|  |  |  +-- js (directory containing all js files from Bootstrap precompiled files)|  +-- manage.py我的設置文件中與靜態文件有關的設置:INSTALLED_APPS = [    'django.contrib.admin',    'django.contrib.auth',    'django.contrib.contenttypes',    'django.contrib.sessions',    'django.contrib.messages',    'django.contrib.staticfiles',    'StaticPages',]STATIC_URL = '/static/'STATICFILES_DIRS = [    os.path.join(BASE_DIR, 'static')]在 StaticPages 應用程序下的 views.py 文件中,我有這個視圖來呈現我的模板:def home(request):    return render(request, 'StaticPages/main.html')所以我們來談談我的問題。在我用來加載靜態文件的 main.html 文件中{% load static %},然后在對 css 或 js 文件的任何調用中,我做了類似的事情,但 pycharm 突出顯示文本并說“無法解析目錄......”這是我的 html 文件的示例:<!DOCTYPE html><html>{% load static %}<head>    <meta charset="UTF-8">    <title>Title</title>    <link rel="stylesheet" href='{% static "/Bootstrap/css/bootstrap.min.css" %}'          integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">    <style>        .navbar-custom{            background-color: #e86813;        }    </style>    <h3>Hello World</h3></head><body>    {% include 'StaticPages/navbar.html' %}</body></html>
查看完整描述

1 回答

?
qq_笑_17

TA貢獻1818條經驗 獲得超7個贊

原來pycharm搞砸了。我上傳了一個免費的 Bootstrap 模板并使用了相同的方法: {% static load %}在我的 html 文件中。

pycharm 再次突出顯示了所有有href="{%static ...directory... %}"錯誤的 html,但是當我啟動服務器時,模板及其所有 css 都顯示為正常,所以 pycharm 在這種情況下表現得很奇怪。


查看完整回答
反對 回復 2023-03-30
  • 1 回答
  • 0 關注
  • 147 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號