7

I'm 2hours stuck in a issue about STATIC_URL and STATIC_ROOT when I try to make run the webapp on my server at webfactional.

when I load the webpage all the requests works well, except by the fact that any link with {{ STATIC_URL}} is working or loading.

So a common error that appears on firebug is:

GET http://mydomain/static/extras/h5bp/js/libs/modernizr-2.5.3.min.js 500 (Internal Server Error) 

My setup is:

urls.py I did nothing, and there's nothing about static files.

settings.py DEBUG = False

STATIC_ROOT = '/home/mydomain/webapps/static_app/'
STATIC_URL = 'http://mydomain/static/'
STATICFILES_DIRS = ()

views.py view example

@csrf_exempt
def IndexView(request):
    try:
        request.user.is_authenticated() 
    except  AttributeError:
        return render_to_response('index.html',
                              {'request': request,},
                              context_instance=RequestContext(request))

    return render_to_response('index.html',
                  {'request': request, 'profile' : request.user},
                  context_instance=RequestContext(request))  

index.html a part of code not found

<script src="{{ STATIC_URL }}extras/h5bp/js/libs/modernizr-2.5.3.min.js"></script>

well, I follow all the points of: https://docs.djangoproject.com/en/1.4/howto/static-files/ and this another one: http://docs.webfaction.com/software/django/getting-started.html

I'm using the correct installed apps, middlewares, template_contexts.

If I'm missing something please help me to figure out.

Thanks in advance!

--edit

I have to say, if I just change the DEBUG = True will works fine.

because on urls.py I have this piece of code:

if settings.DEBUG:
    # static files (images, css, javascript, etc.)
    urlpatterns += patterns('',
        (r'^media/(?P<path>.*)/$', 'django.views.static.serve', {
        'document_root': settings.MEDIA_ROOT}))
4

1 回答 1

11

在开发环境中不需要的生产环境中必须发生 2 件事。

您必须运行manage.py collectstatic- 这会将所有静态文件收集到您的STATIC_ROOT目录中。

您必须在url提供您的STATIC_ROOT目录。STATIC_URL具体如何取决于您的生产设置。这甚至与 django 无关;重要的是STATIC_ROOT内容可在STATIC_URL.

假设您使用的是 Apache,您会将 URL 别名为目录。

Alias /static/ /path/to/my/static_root/

如果您使用的是 nginx,则类似于

location = /static/ {
    alias /path/to/my/static_root/;
}

我刚刚意识到您正在使用 webfaction,在这种情况下,您设置了一个静态应用程序,该应用程序实际上只是在您定义的 URL 的目标目录中提供文件。我正在尝试记住我的 webfaction 登录以查看确切的过程,但这应该不难弄清楚。

更新:登录webfaction;您可以创建一个符号链接的应用程序。简单的!

创建一个服务于 /YOUR_STATIC_URL/ 的符号链接应用程序并将其指向 /YOUR_STATIC_ROOT/。完毕!

于 2012-08-30T19:57:25.320 回答