简体   繁体   English

Django 1.5.4中的STATIC_URL问题

[英]STATIC_URL issue in django 1.5.4

I wanted to enable serving static files through django 1.5 builtin development server. 我想启用通过django 1.5内置开发服务器提供静态文件的功能。 I came across a strange problem. 我遇到一个奇怪的问题。

If I make a request to my static file using localhost:8000/static/staticstyle.css then it responds me with 404 not found. 如果我使用localhost:8000/static/staticstyle.css向我的静态文件发出请求,那么它将以404找不到我。 But If I make a request to the same file without the value mentioned in the STATIC_URL and add the url pattern as "url(r'^anyval_other_than_mentioned_STATIC_URL/', django.views.static.serve,{'document_setting':settings.STATIC_ROOT}), " , then it responds me with 304 , which is conditional get. 但是,如果我对没有STATIC_URL提到的值的同一文件进行请求,并将网址格式添加为"url(r'^anyval_other_than_mentioned_STATIC_URL/', django.views.static.serve,{'document_setting':settings.STATIC_ROOT}), " ,然后它以304响应我,这是有条件的get。

I have collected all the static files in the static directory setup in the settings file and enabled all the context processor required for the template. 我已经在设置文件的静态目录设置中收集了所有静态文件,并启用了模板所需的所有上下文处理器。

For production I have used nginx to serve the static file so there is no problem. 对于生产,我已经使用nginx来提供静态文件,因此没有问题。

My settings.py looks like 我的settings.py看起来像

STATIC_ROOT = APPLICATION_PATH+"/static/"

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    ('vendors',APPLICATION_PATH+'/vendors'),
    ('admin/assets/',APPLICATION_PATH+'/templates/admin/assets'),
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
)

My application urls.py looks like 我的应用程序urls.py看起来像

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),  
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Any hints would be highly appreciated. 任何提示将不胜感激。

Thank you . 谢谢 。

please read this first https://docs.djangoproject.com/en/1.5/howto/static-files/ 请先阅读此https://docs.djangoproject.com/zh-CN/1.5/howto/static-files/

There are some settings related to static files serve The list of finder backends that know how to find static files in various locations. 有一些与静态文件服务相关的设置。查找程序后端列表知道如何在各个位置查找静态文件。

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

Absolute path to the directory static files should be collected to 目录静态文件的绝对路径应收集到

STATIC_ROOT = os.path.join(APPLICATION_PATH, '..', 'static')

URL prefix for static files. 静态文件的URL前缀。

STATIC_URL = '/static/'

You can specify Additional locations of static files 您可以指定静态文件的其他位置

STATICFILES_DIRS = (
    os.path.join(APPLICATION_PATH, 'vendors'),
    os.path.join(APPLICATION_PATH, '/templates/admin/assets'),
)

There is special application for static serve in django contrib Django contrib中有静态服务的特殊应用

INSTALLED_APPS = (
    ...
    'django.contrib.staticfiles',
    ...
)

After You have configured your project run collectstatic command 配置项目后,请运行collectstatic命令

python manage.py collectstatic

This will copy all files from your static folders into the STATIC_ROOT directory. 这会将所有文件从您的静态文件夹复制到STATIC_ROOT目录中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM