如何在 Django 1.3 开发 URL 中提供静态文件和动态文件?

发布于 2024-11-16 23:37:00 字数 397 浏览 3 评论 0原文

我有点难住了。在开发过程中,我尝试在 DJango 1.3 中为我的应用程序提供静态和动态文件。我喜欢新的静态功能,但我似乎无法让它正常工作。

当我阅读文档时,看起来以下内容应该有效。它可以很好地服务于动态的东西,但不是静态的。

urlpatterns += staticfiles_urlpatterns()

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

I'm a little stumped. In development, I'm trying to serve both static AND dynamic files for my app in DJango 1.3. I love the new static features, but I can't seem to get this to work properly.

When I read the docs, it looks like the following should work. It serves dynamic stuff fine, but not static.

urlpatterns += staticfiles_urlpatterns()

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

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

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

发布评论

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

评论(2

得不到的就毁灭 2024-11-23 23:37:00

在 django 1.3 中,静态和动态内容已经分离。要使用新功能,请像这样设置您的项目:

project
 |- app1
 |- media       # exists only on server/folder for dynamic content
 |- static-root # exists only on server/folder for static content
 |- static      # folder for site-specific static content
 |- settings.py
 |- manage.py
 `- urls.py

settings.py

from os import path
PROJECT_ROOT = path.dirname(path.abspath(__file__)) #gets directory settings is in

#-- dynamic content is saved to here --
MEDIA_ROOT = path.join(PROJECT_ROOT,'media')
MEDIA_URL  = '/media/'

#-- static content is saved to here --
STATIC_ROOT = path.join(PROJECT_ROOT,'static-root') # this folder is used to collect static files in production. not used in development
STATIC_URL =  "/static/"
ADMIN_MEDIA_URL = STATIC_URL + 'admin/' #admin is now served by staticfiles
STATICFILES_DIRS = (
    ('site', path.join(PROJECT_ROOT,'static')), #store site-specific media here.
)

#-- other settings --
INSTALLED_APPS = (
    ...
    'django.contrib.staticfiles',
    ...
)

urls.py

from django.conf import settings

#your URL patterns

if settings.DEBUG:
    urlpatterns += staticfiles_urlpatterns() #this servers static files and media files.
    #in case media is not served correctly
    urlpatterns += patterns('',
        url(r'^media/(?P<path>.*)
, 'django.views.static.serve', {
            'document_root': settings.MEDIA_ROOT,
        }),
    )

In django 1.3 static and dynamic content have been separated. to use the new features, set up your project like this:

project
 |- app1
 |- media       # exists only on server/folder for dynamic content
 |- static-root # exists only on server/folder for static content
 |- static      # folder for site-specific static content
 |- settings.py
 |- manage.py
 `- urls.py

settings.py

from os import path
PROJECT_ROOT = path.dirname(path.abspath(__file__)) #gets directory settings is in

#-- dynamic content is saved to here --
MEDIA_ROOT = path.join(PROJECT_ROOT,'media')
MEDIA_URL  = '/media/'

#-- static content is saved to here --
STATIC_ROOT = path.join(PROJECT_ROOT,'static-root') # this folder is used to collect static files in production. not used in development
STATIC_URL =  "/static/"
ADMIN_MEDIA_URL = STATIC_URL + 'admin/' #admin is now served by staticfiles
STATICFILES_DIRS = (
    ('site', path.join(PROJECT_ROOT,'static')), #store site-specific media here.
)

#-- other settings --
INSTALLED_APPS = (
    ...
    'django.contrib.staticfiles',
    ...
)

urls.py

from django.conf import settings

#your URL patterns

if settings.DEBUG:
    urlpatterns += staticfiles_urlpatterns() #this servers static files and media files.
    #in case media is not served correctly
    urlpatterns += patterns('',
        url(r'^media/(?P<path>.*)
, 'django.views.static.serve', {
            'document_root': settings.MEDIA_ROOT,
        }),
    )
剧终人散尽 2024-11-23 23:37:00

检查settings.py中的STATIC_URL,值是多少?

Check STATIC_URL from settings.py, what is the value ?

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