服务器上的Django中的STATIC_ROOT [英] STATIC_ROOT in Django on Server

查看:156
本文介绍了服务器上的Django中的STATIC_ROOT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



当我加载网页时,我在2小时内遇到了有关STATIC_URL和STATIC_ROOT的问题,当我尝试在web服务器上运行webapp。除了{{STATIC_URL}}的任何链接正在工作或加载之外,请求的工作正常。



所以在firebug上出现的常见错误是: p>

  GET http://mydomain/static/extras/h5bp/js/libs/modernizr-2.5.3.min.js 500(内部服务器错误

我的设置是:



urls.py
我没有做任何事情,静态文件也没有什么。



settings.py
DEBUG = False

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

views.py
视图示例

  @csrf_exempt 
def IndexView(request):
try:
request.user.is_authenticated )
除了AttributeError:
返回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未找到代码的一部分

 < script src ={{STATIC_URL}} extras / h5bp / js / libs / modernizr-2.5.3.min.js>< / script>好的,我遵循以下所有要点:$ b​​ $ b  

/docs.djangoproject.com/en/1.4/howto/static-files/rel =noreferrer> https://docs.djangoproject.com/en/1.4/howto/static-files/
和另一个:
http://docs.webfaction。 com / software / django / getting-started.html



我正在使用正确安装的应用程序,中间件,template_contexts。



如果我缺少一些东西,请帮我找出来。



提前感谢!



- 编辑



我不得不说,如果我只是更改DEBUG = True将正常工作。



因为在urls.py上我有这段代码:

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


解决方案

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



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



您必须在 STATIC_URL 网址上提供 STATIC_ROOT 目录。如何完全取决于您的生产设置。这甚至不是django相关的;所有重要的是 STATIC_ROOT 内容可在 STATIC_URL 中。



假设您使用的是Apache,您可以将目录中的URL别名。

 别名/静态/路径/到/ my / static_root / 

如果你使用nginx, / p>

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

我刚刚意识到您正在使用webfaction,在这种情况下,您设置静态应用程序,它只是为您定义的URL上的目标目录提供文件。我想记住我的webfaction登录信息以查看确切的过程,但是不难弄清楚。



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



创建一个符号链接应用程序/ YOUR_STATIC_URL /并将其指向/ YOUR_STATIC_ROOT /。完成!


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

解决方案

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!

这篇关于服务器上的Django中的STATIC_ROOT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆