Django 静态文件再次不起作用

发布于 2024-11-09 03:29:05 字数 2446 浏览 0 评论 0原文

我知道这个问题出现了一百万次,而且我也不是 django 编码新手,最近我在 django 项目中让静态文件工作时遇到了可怕的问题。上次我使用 1.3 解决了这个问题,但现在我正在开发 1.2.5 版本,但没有一个解决方案有效。

所以基本上我的静态数据只在管理部分工作,而在网站的其他地方都没有。 静态文件位于 K:/project/media 中,我使用的是 window、python 2.7 和 django 1.2.5 以及开发服务器。

设置:

MEDIA_ROOT = path.join(path.abspath(path.dirname(__file__)), 'media')
MEDIA_URL = '/static/'
ADMIN_MEDIA_PREFIX = '/admin_media/'

URLconf:

urlpatterns += patterns('',
    (r'^static/(?P<path>.*)$', 'django.views.static.serve',
        {'document_root': 'K:/project/media/', 'show_indexes': True}
    ),
)

现在当我打开 http://127.0.0.1:8000/static/ 而不是静态文件列表 我在 /static/ 处获取 TemplateDoesNotExist。模板中的导入返回类似的错误而不是 404 : TemplateDoesNotExist at /static/css/global.css/ 。我在这里缺少什么?

编辑

完整 URL 配置

from django.conf.urls.defaults import *
from django.views.generic.simple import redirect_to
from p.globals import views
from p.newsevents.feeds import ReleaseFeed
import filebrowser

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

feeds = {
    'releases': ReleaseFeed,
}

urlpatterns = patterns('',

    (r'(?P<reqPath>[\w\-,.]+)$', redirect_to, {'url': '%(reqPath)s/'}),

    (r'^admin/filebrowser/', include('filebrowser.urls')),
    #(r'^admin$', redirect_to, {'url': '/admin/'}),
    (r'^admin/', include(admin.site.urls)),
    (r'^tinymce/', include('tinymce.urls')),
    #(r'^about-us/$', redirect_to, {'url': '/about-us/our-story/'}),
    (r'^feeds/(?P<url>.*)/$', 'django.contrib.syndication.views.feed', {'feed_dict': feeds}),
    (r'^get-your-look/', include('p.getyourlook.urls')),
    (r'^about-us/', include('p.aboutus.urls')),
    (r'^our-services/', include('p.services.urls')),
    (r'^news-events/', include('p.newsevents.urls')),
    (r'^promotions/', include('p.promotions.urls')),
    (r'^careers/', include('p.careers.urls')),
    (r'^locations/', include('p.locations.urls')),

    (r'^$', views.home),
    (r'^(?P<reqPath>[\w\-,.]+)/', views.globals_views),
)

from django.conf import settings

urlpatterns += patterns('',
    (r'^static/(?P<path>.*)$', 'django.views.static.serve',
        {'document_root': 'K:/Kuba/Webdesign/hair/media/', 'show_indexes': True}
    ),
)

I know this question appeared like a milion times, also I'm not a newbie django coder still recently I'm having terrible problems gotting static files work in django projects. Last time I worked around this by using 1.3 but now I'm working on 1.2.5 version and none of the solutions works.

So basically I have statics worknig only in admin section and nowhere else around the site.
Static files are in K:/project/media, I'm using window, python 2.7 and django 1.2.5 and the development server.

Settings :

MEDIA_ROOT = path.join(path.abspath(path.dirname(__file__)), 'media')
MEDIA_URL = '/static/'
ADMIN_MEDIA_PREFIX = '/admin_media/'

URLconf :

urlpatterns += patterns('',
    (r'^static/(?P<path>.*)

Now when I open http://127.0.0.1:8000/static/ instead of static files list I'm getting TemplateDoesNotExist at /static/. And imports in template return similar error instead of 404 : TemplateDoesNotExist at /static/css/global.css/ . What am I missing here ?

EDIT

Full URL conf

from django.conf.urls.defaults import *
from django.views.generic.simple import redirect_to
from p.globals import views
from p.newsevents.feeds import ReleaseFeed
import filebrowser

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

feeds = {
    'releases': ReleaseFeed,
}

urlpatterns = patterns('',

    (r'(?P<reqPath>[\w\-,.]+)
, 'django.views.static.serve',
        {'document_root': 'K:/project/media/', 'show_indexes': True}
    ),
)

Now when I open http://127.0.0.1:8000/static/ instead of static files list I'm getting TemplateDoesNotExist at /static/. And imports in template return similar error instead of 404 : TemplateDoesNotExist at /static/css/global.css/ . What am I missing here ?

EDIT

Full URL conf


, redirect_to, {'url': '%(reqPath)s/'}),

    (r'^admin/filebrowser/', include('filebrowser.urls')),
    #(r'^admin
, 'django.views.static.serve',
        {'document_root': 'K:/project/media/', 'show_indexes': True}
    ),
)

Now when I open http://127.0.0.1:8000/static/ instead of static files list I'm getting TemplateDoesNotExist at /static/. And imports in template return similar error instead of 404 : TemplateDoesNotExist at /static/css/global.css/ . What am I missing here ?

EDIT

Full URL conf

, redirect_to, {'url': '/admin/'}), (r'^admin/', include(admin.site.urls)), (r'^tinymce/', include('tinymce.urls')), #(r'^about-us/ , 'django.views.static.serve', {'document_root': 'K:/project/media/', 'show_indexes': True} ), )

Now when I open http://127.0.0.1:8000/static/ instead of static files list I'm getting TemplateDoesNotExist at /static/. And imports in template return similar error instead of 404 : TemplateDoesNotExist at /static/css/global.css/ . What am I missing here ?

EDIT

Full URL conf

, redirect_to, {'url': '/about-us/our-story/'}), (r'^feeds/(?P<url>.*)/ , 'django.views.static.serve', {'document_root': 'K:/project/media/', 'show_indexes': True} ), )

Now when I open http://127.0.0.1:8000/static/ instead of static files list I'm getting TemplateDoesNotExist at /static/. And imports in template return similar error instead of 404 : TemplateDoesNotExist at /static/css/global.css/ . What am I missing here ?

EDIT

Full URL conf

, 'django.contrib.syndication.views.feed', {'feed_dict': feeds}), (r'^get-your-look/', include('p.getyourlook.urls')), (r'^about-us/', include('p.aboutus.urls')), (r'^our-services/', include('p.services.urls')), (r'^news-events/', include('p.newsevents.urls')), (r'^promotions/', include('p.promotions.urls')), (r'^careers/', include('p.careers.urls')), (r'^locations/', include('p.locations.urls')), (r'^ , 'django.views.static.serve', {'document_root': 'K:/project/media/', 'show_indexes': True} ), )

Now when I open http://127.0.0.1:8000/static/ instead of static files list I'm getting TemplateDoesNotExist at /static/. And imports in template return similar error instead of 404 : TemplateDoesNotExist at /static/css/global.css/ . What am I missing here ?

EDIT

Full URL conf

, views.home), (r'^(?P<reqPath>[\w\-,.]+)/', views.globals_views), ) from django.conf import settings urlpatterns += patterns('', (r'^static/(?P<path>.*) , 'django.views.static.serve', {'document_root': 'K:/project/media/', 'show_indexes': True} ), )

Now when I open http://127.0.0.1:8000/static/ instead of static files list I'm getting TemplateDoesNotExist at /static/. And imports in template return similar error instead of 404 : TemplateDoesNotExist at /static/css/global.css/ . What am I missing here ?

EDIT

Full URL conf

, 'django.views.static.serve', {'document_root': 'K:/Kuba/Webdesign/hair/media/', 'show_indexes': True} ), ), 'django.views.static.serve', {'document_root': 'K:/project/media/', 'show_indexes': True} ), )

Now when I open http://127.0.0.1:8000/static/ instead of static files list I'm getting TemplateDoesNotExist at /static/. And imports in template return similar error instead of 404 : TemplateDoesNotExist at /static/css/global.css/ . What am I missing here ?

EDIT

Full URL conf

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

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

发布评论

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

评论(1

两仪 2024-11-16 03:29:05

我猜是因为这个:

 (r'^(?P<reqPath>[\w\-,.]+)/', views.globals_views),

它会拦截所有对静态的请求,并调用该视图,这将导致 TemplateDoesNotExist 错误。
在此之前放置一个静态网址或更改此(及其引用的网址)正则表达式。

I guess it`s because of this:

 (r'^(?P<reqPath>[\w\-,.]+)/', views.globals_views),

It will intercept all request to static, and will call that view, which will cause TemplateDoesNotExist error.
Put a static url before this or change this( and in urls that it refers to) regex.

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