-1

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="/assets/css/admin.css") and it doesn't find it. I tried to edit the settings.py 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 settings.py in order to have the following structure and to be able to link my files normally?

Project

----Site

----Templates

--------Admin

--------Site

----Assets

--------Img

--------Css

--------Js

Thanks in advance!

jpic
  • 32,891
  • 5
  • 112
  • 113

3 Answers3

1

Extract from 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:

>>> ./manage.py findstatic admin/css/base.css
Found 'admin/css/base.css' here:
  /home/jpic/env/lib/python2.7/site-packages/django/contrib/admin/static/admin/css/base.css

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/ has priority over django/contrib/admin/static, because of the default order of settings.STATICFILES_FINDERS.

So, collectstatic will get /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

Where:

  • /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.

Hope this helps.

jpic
  • 32,891
  • 5
  • 112
  • 113
  • 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 Aug 28 '12 at 13:22
  • 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 Aug 28 '12 at 13:39
  • "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](https://github.com/yourlabs/django-autocomplete-light/tree/master/autocomplete_light/static/autocomplete_light). "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 Aug 28 '12 at 15:17
0

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

Jingo
  • 3,200
  • 22
  • 29
0

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 and 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 here to see an example of using the Django serve() function to do this.

In production, you just need to configure your server to look in that directory for anything starting with /assets/ in the URL path.

Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107
Jason Mayfield
  • 121
  • 1
  • 2