V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
bb2018
V2EX  ›  Python

py2.7.12 + django1.11 版 本机静态文件配置问题

  •  
  •   bb2018 · 2017-07-03 21:56:41 +08:00 · 2056 次点击
    这是一个创建于 2482 天前的主题,其中的信息可能已经有所发展或是发生改变。

    win xp 系统  python2.7.12 django 1.11 版本

    settings.py 里配置: STATIC_URL = '/statics/' STATICFILES_DIRS = (os.path.join(BASE_DIR, "statics"),)

    静态文件存放于: 根目录 /statics/bbs/ 下面

    根目录:templates/bbs/index.html  里{% load static %} 看有人在模板里用

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

    <img src="{% static "bbs/aaa.png" %}">

    现在打开首页发现 jpg, png 等图片文件可以在首页正常显示。通过网址: http://127.0.0.1:8000/statics... 可正常访问。

    但是.css,js 等文件首页不能正常加载。 但是可以通过网址: http://127.0.0.1:8000/statics... 正常访问与下载

    看到有些教程说是在 app 应用下面建 static 文件。但是上面我设置的不知哪里不对?应该怎么处理好?

    不知是什么原因? 没有配置好?

    5 条回复    2017-07-04 16:10:49 +08:00
    bestkayle
        1
    bestkayle  
       2017-07-04 09:06:17 +08:00
    {% static " bbs="" style.css"="" %},这里面的等于号啥意思。。。
    bb2018
        2
    bb2018  
    OP
       2017-07-04 10:16:35 +08:00
    不好意思。应该是这样:<link rel="stylesheet" type="text/css" media="screen" href="{% static " bbs/ style.css" %}">

    我复制过来。不知道就变成了那样乱了码。
    @bestkayle
    lk5280
        3
    lk5280  
       2017-07-04 10:37:55 +08:00
    ```python
    # setting.py
    STATIC_URL = '/static/'
    STATIC_ROOT = os.path.join(BASE_DIR, 'static')

    # xx.html
    {% load staticfiles %}

    # use in xx.html
    <link rel="stylesheet" href="{% static '[staticfile path]' %}">
    ```
    bb2018
        4
    bb2018  
    OP
       2017-07-04 16:05:54 +08:00
    @lk5280
    按你说的设置。STATIC_ROOT = os.path.join(BASE_DIR, 'static') 加这句不知是什么作用总是不行。

    这样设置后:
    STATIC_URL = '/static/'
    # STATIC_ROOT = os.path.join(BASE_DIR, 'static')
    STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
    )

    别的按你说的设置。
    图片可以正常加载。但是 css 文件不行。
    bb2018
        5
    bb2018  
    OP
       2017-07-04 16:10:49 +08:00
    @lk5280

    这是首页生成的源代码。下面的图片地址可以正常出现。但是下面的 css 代码就不行了。

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>社区</title>
    <link rel="stylesheet" href="/static/bbs/css/style.css">
    </head>
    <body>
    <img src="/static/bbs/img/logo.png">
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   964 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 19:41 · PVG 03:41 · LAX 12:41 · JFK 15:41
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.