python - 链接到 Django 1.3 中的静态文件的问题

标签 python django static-files

我在 Windows XP 上运行 django 1.3,python 2.7

我正在尝试在我的 Django 应用程序的静态文件夹中设置一个 css。

模板看起来像:

<html>
    <head>
        <title>css demo</title>
        <link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}css/my.css" />

    </head>
    <body>

生成的 HTML 如下所示:

<html> 
    <head> 
        <title>css demo</title> 
        <link rel="stylesheet" type="text/css" href="http://localhost/static/css/my.css" /> 

    </head>
...

看来我已经设置好所有内容以指定静态文件在模板中的位置,然后在生成的 html 中。

但是在 ' http://localhost/static/css/my.css 处有注释'.我如何获得它?

我像这样运行了 collectstatic:

C:\root\workspace\mywebapp\src\mywebapp>c:\Python27\python.exe manage.py collectstatic

You have requested to collect static files at the destination location as specified in your settings file.

This will overwrite existing files.
Are you sure you want to do this?

Type 'yes' to continue, or 'no' to cancel: yes
Copying 'c:\root\workspace\mywebapp\src\mywebapp\css_demo\static\css\my.css'

1 static file copied to 'C:/root/workspace/mywebapp/src/mywebapp/static/'.

所以我现在在 c:\root\workspace\mywebapp\src\mywebapp\css_demo\static\css\my.css 中有 my.css

在我的设置中,我有:

STATIC_ROOT = 'C:/root/workspace/mywebapp/src/mywebapp/static/'
STATIC_URL = 'http://localhost/static/'

在我的 url.py 中我有:

from django.conf.urls.defaults import patterns, include, url
from django.contrib.staticfiles.urls import staticfiles_urlpatterns

urlpatterns = patterns('',

    (r'^mywebapp/', include('mywebapp.css_demo.urls')),
)

urlpatterns += staticfiles_urlpatterns()

所以如果我没理解错的话,我的 css 在:

c:\root\workspace\mywebapp\src\mywebapp\css_demo\static\css\my.css

不应该在:

http://localhost/static/css/my.css

但我看到的是:

Page not found (404)
Request Method: GET
Request URL:    http://localhost/static/css/my.css
Using the URLconf defined in mywebapp.urls, Django tried these URL patterns, in this order:
^mywebapp/
The current URL, static/css/my.css, didn't match any of these.

我也试过:

http://localhost/mywebapp/static/css/my.css
http://localhost/mywebapp/css_demo/static/css/my.css

我是不是漏掉了一步? 关于此的文档有点令人困惑。 http://docs.djangoproject.com/en/1.3/howto/static-files/ http://docs.djangoproject.com/en/1.3/ref/contrib/staticfiles/

谢谢!

最佳答案

我打赌你的应用运行在http://localhost:8000,而不是http://localhost :)

改变

STATIC_URL = 'http://localhost/static/'

STATIC_URL = '/static/'

那里不需要绝对 URL。

关于python - 链接到 Django 1.3 中的静态文件的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6073916/

相关文章:

python - Selenium Python : Download PDF specified by DOM object

Python 引用调用问题

python sklearn : what is the difference between accuracy_score and learning_curve score?

python - 以 django 模型形式手动输入值

python - 在 Django 中提供静态内容会导致 CSS 文件出现 404 错误

python - django 1.11 中找不到静态文件

python - 循环遍历 pandas 数据框行以应用条件的替代方案?

python - Django 1.8.5 迁移根本不迁移到 Postgres 数据库

django - VS Code 不使用 pipenv .env 文件

python - 为django中的所有模板加载静态文件