STATIC_ROOT in Django on Server

百般思念 提交于 2019-12-18 12:34:17

问题


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}))

回答1:


2 things must happen on a production environent that is not needed in the development environment.

You must run manage.py collectstatic -- this collects all static files into your STATIC_ROOT directory.

You must serve your STATIC_ROOT directory at the STATIC_URL url. How exactly depends on your production setup. This is not even django related; all that matters is that STATIC_ROOT contents are available at STATIC_URL.

Let's say you're using Apache, you'd alias a URL to a directory.

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

If you're using nginx, it would be something like

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

I just realized you're using webfaction, in which case you set up a static application which literally just serves files at the targeted directories at the URL you define. I'm trying to remember my webfaction login to see the exact procedure, but it shouldn't be difficult to figure out.

Update: Logged into webfaction; you can create an application that is a symlink. Easy!

Create a symbolic link app that serves /YOUR_STATIC_URL/ and point it to /YOUR_STATIC_ROOT/. Done!



来源:https://stackoverflow.com/questions/12204136/static-root-in-django-on-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!