Python Django 管理员不提供静态文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23728366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:23:45  来源:igfitidea点击:

Django admin not serving static files?

pythondjangostaticadmin

提问by allanberry

Django 1.6

Django 1.6

I'm having trouble serving my static files for my Django Admin.

我在为 Django 管理员提供静态文件时遇到问题。

urls.py:

网址.py:

urlpatterns = patterns('',
    url(r'^$', 'collection.views.index', name='home'),
    url(r'^collection/', include('collection.urls')),
    url(r'^admin/',    include(admin.site.urls)),
)

if settings.DEBUG:
    urlpatterns += patterns('',
        url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
            'document_root': settings.MEDIA_ROOT,
        }),
        url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {
            'document_root': settings.STATIC_ROOT,
        }),
)

settings.py

设置.py

...
MEDIA_ROOT = '/Users/me/projectdir/media/'
MEDIA_URL = 'media/'

STATIC_ROOT = '/Users/me/projectdir/static/'
STATIC_URL = 'static/'
...

template (base.html)

模板 (base.html)

<!DOCTYPE html>
<html lang='en-us'>
<head>
<title>Mysite</title>

{% load static %}
{% block links %}
    <link href="{% static 'css/bootswatch-simplex.css' %}" rel="stylesheet" type="text/css">
    <link href="{% static 'css/custom.css' %}" rel="stylesheet" type="text/css">
    <link rel="shortcut icon" href="{% static "favicon.ico" %}">
{% endblock %}

<script src="{% static "lib/bootstrap-3.1.1-dist/js/bootstrap.js" %}"></script>
<script type="text/javascript">window.__admin_media_prefix__ = "{% filter escapejs %}{% static "admin/" %}{% endfilter %}";</script>
</head>
...

Django is serving my admin OK, just without static files: CSS, JS, etc.

Django 正在为我的管理员服务,只是没有静态文件:CSS、JS 等。

Static files for my public-facing pages work fine.

我面向公众的页面的静态文件工作正常。

If I change STATIC_URLto '/static/', then the oppositeis true: the admin is fine, but my public pages lose their static files.

如果我更改STATIC_URL'/static/',则相反:管理员很好,但我的公共页面丢失了它们的静态文件。

Here's the weirdest part. If I "view source" of my admin pages in my browser, it shows the correct URL for static pages, for example:

这是最奇怪的部分。如果我在浏览器中“查看源”管理页面,它会显示静态页面的正确 URL,例如:

/static/admin/css/base.css

But if I actually follow the link, it changes it to this:

但是,如果我真的按照链接操作,它会将其更改为:

http://localhost:8000/admin/static/admin/css/base.css

I think it's checking for static files relative to localhost:8000/admin/static/instead of just localhost:8000/static/. It adds an extra "admin" level to the url, like staticis part of the domain. I just can't figure out how to get rid of it.

我认为这是检查对静态文件相对localhost:8000/admin/static/的,而不是只localhost:8000/static/。它admin为 url添加了一个额外的“ ”级别,就像static是域的一部分。我只是不知道如何摆脱它。

I have tried collectstatic, but it doesn't help. The static files are in my static directory, they're just not being served. I can type in, say, http://localhost:8000/static/admin/css/base.cssand I get the right CSS file (in plaintext). The files are there. I bet something is wrong with my configuration.

我试过了collectstatic,但没有帮助。静态文件在我的静态目录中,只是没有被提供。比如说,我可以输入,http://localhost:8000/static/admin/css/base.css然后我得到正确的 CSS 文件(纯文本)。文件在那里。我敢打赌我的配置有问题。

I've emptied my caches, restarted my dev server, etc. No beans.

我清空了我的缓存,重新启动了我的开发服务器等。没有 bean。

ideas?

想法?

采纳答案by allanberry

OK, I figured it out. There was some confusion in my settings files, and I did not have STATICFILES_DIRScorrectly set.

好的,我想通了。我的设置文件中有一些混乱,我没有STATICFILES_DIRS正确设置。

In the end, I implemented the version-controlled settings files discussed in Two Scoops of Django 1.6, with this in my settings:

最后,我实现了两勺 Django 1.6 中讨论的版本控制设置文件,在我的settings

from unipath import Path

BASE_DIR         =  Path(__file__).ancestor(3)
MEDIA_ROOT       =  BASE_DIR.child('media')
STATIC_ROOT      =  BASE_DIR.child('static')

TEMPLATE_DIRS    = (
    BASE_DIR.child('templates'),
)

STATICFILES_DIRS = (
    BASE_DIR.child('myapp').child('static'),
)

STATIC_URL         = '/static/'
MEDIA_URL          = '/media/'

With this, my static files are being served correctly, both in admin and without. My media files, on the other hand, did not work without changing my urls.pyin development, according to the accepted answer here. I did not have to do the same for my static files.

有了这个,我的静态文件在管理员和没有被正确提供。另一方面urls.py,根据此处接受的答案,我的媒体文件在不更改我的开发中的情况下无法正常工作。我不必为我的静态文件做同样的事情。

Anyways, I hope this helps anyone else banging their head against this particular wall.

无论如何,我希望这可以帮助其他任何人将他们的头撞在这堵墙上。

回答by ruddra

Use django-admin.py collectstaticor go to ~/django/contrib/admin/staticand copy the admin folder(which contains the static files) and paste them into your project's static directory.

使用django-admin.py collectstatic或转到~/django/contrib/admin/static并复制 admin 文件夹(其中包含静态文件)并将它们粘贴到项目的静态目录中。

**EDIT**

**编辑**

A desperate or clumsy solution you can try for: change your STATIC_URL to '/static/', as from question I saw this:

您可以尝试一个绝望或笨拙的解决方案:将您的 STATIC_URL 更改为“/static/”,正如我从问题中看到的那样:

If I change STATIC_URL to '/static/', then the opposite is true: the admin is fine, but my public pages lose their static files.

如果我将 STATIC_URL 更改为“/static/”,则相反:管理员很好,但我的公共页面丢失了它们的静态文件。

Then check with inspect element/firebug, see what urls are being served in public pages. Probably a '/' missing or added a '/'. Adjust it, and see if it works.

然后检查inspect element/firebug,查看公共页面中提供的网址。可能缺少“/”或添加了“/”。调整它,看看它是否有效。

回答by ABB

I faced the same issue for two times. The way i solved it was by pasting the static files of admin into static folder mentioned in the code -

我两次遇到同样的问题。我解决它的方法是将 admin 的静态文件粘贴到代码中提到的静态文件夹中 -

cp -r /usr/local/lib/python2.7/site-packages/django/contrib/admin/static/admin /home/ec2-user/mywork-Deployment/mywork/static

This one definitely works and saves a lot of time and troubles. Hope it helps!

这绝对有效,可以节省大量时间和麻烦。希望能帮助到你!

回答by ABB

The way that worked out for me was I referenced the static admin files in my settings.py file. I hope this helps someone :)

对我有用的方法是我在 settings.py 文件中引用了静态管理文件。我希望这可以帮助别人 :)

'./static/admin/',