本文介绍了在django中访问static的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我无法排序我的静态目录并通过django通过html页面中的模板链接css文件。我一直收到错误Not Found:/CSS/bootstrap.min.css 我知道这是我的目录在settings.py中设置的问题但我似乎无法解决这个问题。以下是我对settings.py和layout.html的代码(我使用的是调用css文件的页面)。 layout.html {%load staticfiles%} <!DOCTYPE html> < html lang =enxmlns =http://www.w3.org/1999/xhtml> < head> < meta charset =utf-8/> < title>测试{%block title%} {%endblock%}< / title> < link href ={%static'css / bootstrap.min.css'%}type =text / css =stylesheet> < / head> < body> < div class =container> {%block content%} {%endblock%} < / div> < / body> < / html> settings.py BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__ file__)))#静态文件(CSS,JavaScript,图片)#https ://docs.djangoproject.com/en/1.10/howto/static-files/ STATIC_URL ='C:/ Users / Luke / Desktop / Capstone / CapstoneNotespool / capstonenotespool / capstonenotespool / static /' STATICFILES_DIRS = [ os.path.join(BASE_DIR,static),] pre> 解决方案我认为你必须再次检查你的静态url,我认为你的配置是错误的。 这里是你要找的答案。 树文件示例 这是我的配置 STATIC_URL ='/静态/' 如果DEBUG: MEDIA_URL ='/ media /' STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR),static,>static-only) MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR),static,>media) STATICFILES_DIRS =( os.path.join(os.path.dirname(BASE_DIR),static,static), ) I'm having trouble sorting my static directory and linking css files through templates in html pages with django. I keep getting the error "Not Found: /CSS/bootstrap.min.css" I know this is a problem with how my directory is set up in settings.py but I can't seem to fix the issue. Below is my code for settings.py and layout.html (the page i'm using the call the css file).layout.html{% load staticfiles %}<!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml"><head> <meta charset="utf-8" /> <title>Testing {% block title %}{% endblock %}</title> <link href="{% static 'css/bootstrap.min.css' %}" type="text/css" rel="stylesheet"></head><body> <div class="container"> {% block content %} {% endblock %} </div></body></html>settings.pyBASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))# Static files (CSS, JavaScript, Images)# https://docs.djangoproject.com/en/1.10/howto/static-files/STATIC_URL = 'C:/Users/Luke/Desktop/Capstone/CapstoneNotespool/capstonenotespool/capstonenotespool/static/'STATICFILES_DIRS = [ os.path.join(BASE_DIR, "static"),] 解决方案 I think You must check again your static url, I think you config be wrong.Here is answer you looking for. Example of tree file And this is my config for this STATIC_URL = '/static/' if DEBUG: MEDIA_URL = '/media/' STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static", >"static-only") MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static", >"media") STATICFILES_DIRS = ( os.path.join(os.path.dirname(BASE_DIR), "static", "static"), ) 这篇关于在django中访问static的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-30 11:33