English 中文(简体)
我对Django静态档案做了什么错误?
原标题:What am I doing wrong with Django static files?

我是第一次撰写达詹戈时,如果我对事情稍感 behind的话,我就这样说了。

这里摘自我的背景。 py:

STATIC_ROOT = os.getcwd().replace( \ , / ) +  /static 

STATIC_URL =  /static_files/ 

STATICFILES_DIRS = (
    os.getcwd().replace( \ , / ) +  /static 
)

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

注:我希望os.getcwd(>.行文。 我相信,这不是我的问题,但请允许我知道,这是一个问题。 在我部署时,它为当地发展中国家的利益而占据了位置,因此,在我部署时,它不会对留在那里感到担忧。

My first issue is that template variables don t seem to be working.

An excerpt from my main template file:

<link rel="stylesheet" type="text/css" href="{{ STATIC_URL|default: /static_files/  }}css/base.css" />

我最初刚刚在这里有<条码>{ STAT_URL ± /code>,但这只是没有回去,因此我尝试增加<条码>。 这确实成功地产生了由此产生的超文本的特制,但还是做了一些工作,使我注意到我的第二个(更重要、更诚实)问题。

I can t get static files to work at all. I have tried several different methods here. I have tried putting absolute and relative paths (in various combinations) in each of the above STATIC_* variables, I have tried using the equivalent MEDIA_URL vars instead, and I have tried putting the following into my urls.py:

urlpatterns = (  ,

    # ...

    (r ^static_files/(?P<path>.*)$ , 
         serve , {
             document_root :  /path/to/django/dir/static_files/ ,
             show_indexes : True 
        }
    ),
)

(I grabbed the above snippet from http://www.arthurkoziel.com/ 2008/09/02/handling-static-files-django/)

现在,我要指出,我希望,一旦最初的杰夫完成,我将最终从一个平行的阿帕奇进程获得固定的档案,但我仍然真心想知道我做错做什么。 我在网上或StackOverflow的静态档案中找到了一种像样的全面的理论。

非常感谢所有帮助。

http://www.un.org。 如果有重要的话,我正在视窗7,使用2.7。

最佳回答

一种可能性 - STATIC_URL获胜 t,除非你重新通过请求书刊。

在你看来,确保你有这样的情况:

return render_to_response(template, context, context_instance=RequestContext(request))

到Django1.3为止,你还可以使用新的捷径:

return render(request, template, context)

此外,确保你具备处理程序者的能力。


EDIT:对第二个问题的可能答案,尝试改变

STATICFILES_DIRS = (
    os.getcwd().replace( \ , / ) +  /static 
)

纽约总部

STATICFILES_DIRS = (
    os.getcwd().replace( \ , / ) +  /static ,
)

EDIT 2. 更为可能的解决方案

You can delete STATICFILES_FINDERS. You only have it set 纽约总部the default, so unless you intend 纽约总部expand it later on, get rid of it (one less thing 纽约总部go wrong).

You can also get rid of the urlpatterns entry. Not necessary.

Your STATIC_ROOT is probably incorrect. This should be the place where Django will gather all the static files from across your project (including the directories described in STATICFILES_DIRS) when you move 纽约总部production.

An example from a typical settings.py of mine:

PROJECT_DIR = os.path.dirname(__file__)

STATIC_URL =  /static/ 

STATICFILES_DIRS = (
    os.path.join(PROJECT_DIR,  static ),
)

STATIC_ROOT =  /var/www/myproject_staticfiles/ 

!!

If you want 纽约总部test whether Django is finding and placing your static files correctly, try running

python manage.py collectstatic

In my case, that would go through my project directories, find all the static files, then copy them across 纽约总部 /var/www/myproject_staticfiles/ , where I can host them with apache.

The Django dev server automagically serves static files from within your project folder, so no collect_static is required while in dev. You don t even need 纽约总部set STATIC_ROOT until you go in纽约总部production.

问题回答

暂无回答




相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

热门标签