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

发布于 2024-11-08 06:04:54 字数 2811 浏览 0 评论 0原文

我在 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 at:

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-文件/ http://docs.djangoproject.com/en/1.3/ref/contrib/静态文件/

谢谢!

I'm running django 1.3, python 2.7 on windows XP

I'm trying to setup a css in a static folder for my django app.

The template looks like:

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

    </head>
    <body>

The generated HTML looks like:

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

    </head>
...

So it appears that I have everything set up in order to specify where the static files are in the template, and then in the generated html.

But there is noting at 'http://localhost/static/css/my.css'. How do I get it there?

I ran collectstatic like so:

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/'.

So I now have my.css in c:\root\workspace\mywebapp\src\mywebapp\css_demo\static\css\my.css

In my settings, I have:

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

and in my url.py I have:

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()

So if I understand correctly, my css at:

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

should no be available at:

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

But instead I see:

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.

I also tried:

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

Am I missing a step here?
The documentation on this is a bit confusing.
http://docs.djangoproject.com/en/1.3/howto/static-files/
http://docs.djangoproject.com/en/1.3/ref/contrib/staticfiles/

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

腻橙味 2024-11-15 06:04:54

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

更改

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

STATIC_URL = '/static/'

“不需要绝对 URL”。

I bet you have your app running at http://localhost:8000, not http://localhost :)

Change

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

to

STATIC_URL = '/static/'

No need for absolute URL there.

囚我心虐我身 2024-11-15 06:04:54

我创建了一个文件夹:

C:\root\workspace\mysite\src\mysite\common

在我的设置中,我有:

STATIC_ROOT = 'C:/root/workspace/mysite/src/mysite/static/'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    "C:/root/workspace/mysite/src/mysite/common/static",
)

这似乎有效。

I created a folder:

C:\root\workspace\mysite\src\mysite\common

And in my settings, I have:

STATIC_ROOT = 'C:/root/workspace/mysite/src/mysite/static/'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    "C:/root/workspace/mysite/src/mysite/common/static",
)

This seems to work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文