Django y su forma extraña de lidiar con css, js e imágenes

My question: Can I simply ignore all of that nonsense of static, media, collectstatic and god knows what and simply link my css, img and my js files normally?

Take the admin files as for an example. I copied the templates into my projects template folder to edit them. Simple enough, but then when I wanted to change the css... Oh boy. I can't link any css files! Only css this template is willing to find is located in my django sources... Change that to anything (like href="/es/assets/css/admin.css") and it doesn't find it. I tried to edit the file and that static whatever thingy over there; this obviously didn't work.

Please help me: Excactly what do I need to write to my in order to have the following structure and to be able to link my files normally?










Gracias de antemano!

preguntado el 28 de agosto de 12 a las 12:08

3 Respuestas

Extraer de Surviving django.contrib.staticfiles (or: how to manage static files with django) :

How to override a static file

Imagine that you want to override /static_url/admin/css/base.css. The first thing you have to do is find its location:

>>> ./ findstatic admin/css/base.css
Found 'admin/css/base.css' here:

Then, copy it to your /srv/project/static_dir/, which you have added to settings.STATICFILES_DIRS (see previous paragraph), for example:

mkdir -p /srv/project/static_dir/admin/css/
cp /home/jpic/env/lib/python2.7/site-packages/django/contrib/admin/static/admin/css/base.css /srv/project/static_dir/admin/css

Your copy would reside in /srv/project/static_dir/admin/css/base.css. For collectstatic, /srv/project/static_dir/ tiene prioridad sobre django/contrib/admin/static, because of the default order of settings.STATICFILES_FINDERS.

¿Entonces collectstatic obtendrá /srv/project/static_url/admin/css/base.css to be a copy of your override /srv/project/static_dir/admin/css/base.css, instead of the original django/contrib/admin/static/admin/css/base.css


  • /static_url/ is STATIC_URL
  • /srv/project/static_root/ is STATIC_ROOT
  • /srv/project/static_dir/ is a dir listed in STATICFILES_DIRS

Also, you will have to run collectstatic when you deploy.

Yes I wrote this article because I think it can help a lot of persons who would rather read a short article that only covers most common use cases rather than the complete documentation - ie. deadlines.

Espero que esto ayude.

Respondido 28 ago 12, 13:08

Thanks! Unfortunately I didn't get this to work. I followed the steps you wrote, but after "collectstatic" I ended up having "static_root" folder with the files from "static_dir" AND a folder "admin" with all it's content, including the very same files I already had in "static_dir". However, I can't use any of these, it still fetches the css file from django folder... I just don't get this static files thing, nor the benefits of it and I've already lost half a day with this, so I think it's time to give up and move back to php to get the job done. Thanks anyways! - user1000736

Hah! Just as I was about to delete all Django related folders, I noticed I had misspelled "os.path.join(SITE_ROOT_DIR,'static_dir')" (I had written '/admin/css' after 'static_dir'). Fixed that and tried the whole thing again and what do you know - It bloody well worked! Now I have my css files in my static_dir. But how does Django decide when to use a file from static_dir and when from static_root? Anyway, what's the difference and why are these files in two seperate places to start with? But the main thing is that it works, so thanks again! - user1000736

"how does Django decide when to use a file from static_dir and when from static_root?" -> Django copies from static_dir to static_root. "why are these files in two seperate places to start with?" -> because this allow django apps to ship their own static files, for example django-autocomplete-light ships its static files. "I just don't get this static files thing, nor the benefits of it and I've already lost half a day with this" you'll see don't worry B) - jpic

No olvides cierra la pregunta when you're done with it ;) - jpic

In the development environment you can serve these files via Django (django.contrib.archivos estáticos). In production, you can simply serve your static files via your webserver like Apache (you could configure an Alias for your static files directory).

Respondido 28 ago 12, 12:08

Yes, you can ignore all of the in-built Django machinery for static, media, etc. Although it really is convenient once you get a handle on it.

But if you do want to ignore it, and serve up the assets in your assets folder, there are two separate environments to consider: development y production.

In development, you can have the Django runserver serve static assets just by telling it where to find them. Take a look at the docs aquí to see an example of using the Django serve() función para hacer esto.

In production, you just need to configure your server to look in that directory for anything starting with /assets/ en el capítulo respecto a la URL camino.

Respondido 28 ago 12, 21:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.