Django:STATIC_URL 将应用程序名称添加到 url

发布于 2024-12-03 06:20:00 字数 1600 浏览 1 评论 0原文

我已经像这样配置了我的静态设置:

STATIC_ROOT = os.path.join(SITE_ROOT, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    ('js', os.path.join(STATIC_ROOT, 'js')),
    ('css', os.path.join(STATIC_ROOT, 'css')),
)

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#   'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

在我的 urls.py 中:

urlpatterns = patterns('',
    url(r'^login/?$', login, name='login'),
    url(r'^logout/?$', logout_then_login, name='logout'),

    url(r'^profile/(?P<user_id>\d+)$', 'profiles.views.detail'),
    url(r'^profile/edit$', 'profiles.views.edit'),
)

urlpatterns += staticfiles_urlpatterns()

它对于 url localhost:8000/login 非常有效,但是当我到达localhost:8000/profile/edit 站点,由我的 profiles 应用程序处理,{{ STATIC_URL }} 更改了所有路径/static/.../profile/static/...,所以我的 javascript 和样式表再也找不到了。

我做错了什么?

编辑:这是我的base.html

<!DOCTYPE html>
<html>
    <head>
        <title>Neighr{% block title %}{% endblock %}</title>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
        <script type="text/javascript" src="{{ STATIC_URL }}js/jquery.min.js"></script>
        {% block script %}{% endblock %}
    </head>
    <body>
        {% block content %}{% endblock %}
    </body>
</html>

I have configured my static settings like so:

STATIC_ROOT = os.path.join(SITE_ROOT, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    ('js', os.path.join(STATIC_ROOT, 'js')),
    ('css', os.path.join(STATIC_ROOT, 'css')),
)

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#   'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

and these in my urls.py:

urlpatterns = patterns('',
    url(r'^login/?

It works very well for the url localhost:8000/login, but when I get to the localhost:8000/profile/edit site, which is handled by my profiles App, the {{ STATIC_URL }} changes all the paths from /static/... to /profile/static/..., so my javascripts and stylesheets are not found any more.

What'd I do wrong?

EDIT: Here would be my base.html

<!DOCTYPE html>
<html>
    <head>
        <title>Neighr{% block title %}{% endblock %}</title>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
        <script type="text/javascript" src="{{ STATIC_URL }}js/jquery.min.js"></script>
        {% block script %}{% endblock %}
    </head>
    <body>
        {% block content %}{% endblock %}
    </body>
</html>
, login, name='login'), url(r'^logout/?

It works very well for the url localhost:8000/login, but when I get to the localhost:8000/profile/edit site, which is handled by my profiles App, the {{ STATIC_URL }} changes all the paths from /static/... to /profile/static/..., so my javascripts and stylesheets are not found any more.

What'd I do wrong?

EDIT: Here would be my base.html


, logout_then_login, name='logout'),

    url(r'^profile/(?P<user_id>\d+)

It works very well for the url localhost:8000/login, but when I get to the localhost:8000/profile/edit site, which is handled by my profiles App, the {{ STATIC_URL }} changes all the paths from /static/... to /profile/static/..., so my javascripts and stylesheets are not found any more.

What'd I do wrong?

EDIT: Here would be my base.html


, 'profiles.views.detail'),
    url(r'^profile/edit

It works very well for the url localhost:8000/login, but when I get to the localhost:8000/profile/edit site, which is handled by my profiles App, the {{ STATIC_URL }} changes all the paths from /static/... to /profile/static/..., so my javascripts and stylesheets are not found any more.

What'd I do wrong?

EDIT: Here would be my base.html


, 'profiles.views.edit'),
)

urlpatterns += staticfiles_urlpatterns()

It works very well for the url localhost:8000/login, but when I get to the localhost:8000/profile/edit site, which is handled by my profiles App, the {{ STATIC_URL }} changes all the paths from /static/... to /profile/static/..., so my javascripts and stylesheets are not found any more.

What'd I do wrong?

EDIT: Here would be my base.html

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

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

发布评论

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

评论(1

娇纵 2024-12-10 06:20:00

由于您使用的是 django 内置开发服务器,请尝试从 urls.py 中删除以下行:

urlpatterns += staticfiles_urlpatterns()

在生产中,您最好不要使用 django 提供静态文件,因此请使用 <代码>collectstatic命令。

编辑

如果settings.py,请尝试如下操作:

STATIC_ROOT = os.path.join(os.path.dirname(__file__), '../../static')
STATIC_URL = '/static/'

Since you're using the django built-in developement server, try to remove the following line from your urls.py:

urlpatterns += staticfiles_urlpatterns()

In production, you'de better not serve static files with django, so use the collectstatic command.

edit

If settings.py, try something like this:

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