0

I have always wondered how static files are found by server when using Django. Recently I deployed my Django app to a server and I had trouble deploying static files. I always get the error as following :

Using the URLconf defined in cms.urls, Django tried these URL patterns, in this order:
^gre/
The current URL, static/, didn't match any of these.

when I asked for my css file like this http://gnijuohz.com/static/css/bootstrap.css

SO 1)

Do I have to do something with my urls.py so that it can find my css files and js files?

Notice that I don't have the root permission on the server,so I can't use the collectstatic command provided by Django.

I did set the STATIC_ROOT and STATIC_URL in my setting.py so that the STATIC_ROOT point to my static files.

2)

I am not clear how the whole process works. When I request my css file, does it look through the urls.py to do matches?

Thanks for any help!

Simeon Visser
  • 118,920
  • 18
  • 185
  • 180
Gnijuohz
  • 3,294
  • 6
  • 32
  • 47

1 Answers1

1

1.

No, Django should not be serving your static files on a production server. Apache (or whatever server you use) should serve the static files, often found in a directory /static/. That's what collectstatic does: it collects all the static files from all your Django apps and it puts them in STATIC_ROOT so your server can serve the files.

Locally you can serve the static files by adjusting your urlpatterns in urls.py.

It's a pity that you can't use collectstatic because that's how Django is intended to be used.

2.

As said above, that's not what should happen.

Now, to answer your question, you could create symbolic links in your STATIC_ROOT to all the locations where your static files are located. So a symbolic link to each static directory of each Django app.

Simeon Visser
  • 118,920
  • 18
  • 185
  • 180
  • That's possible if that works. But you may wish to automate it, so you could write a little script that locally collects the static files before uploading the code to your server. The final result would be the same: as long as the static files can be found by the server, they can be served from that directory. – Simeon Visser Jun 05 '12 at 17:39