[Answered ]-Django and staticfiles

1πŸ‘

βœ…

In production the webserver serves your static files. Therefore it is important to move/copy the files to a location where the server then directs to. That you can do with the comfy command python manage.py collectstatic. This you probably knew and have already done.

Depending on the folder structure the files end up with a different structure within created folder for the staticfiles.
Given the following structure:

myproject/
 - myapp1/
   - static/
     - myapp1/
       - css/
         - styles.css

Remember to give it namespacing, meaning within the static directory of myapp1 you create another directory called the same as the app (myapp1). When you run collectstatic now the staticfiles end up at <STATIC_ROOT>/myapp1/css/styles.css and you would address them in your template with href="{% static 'myapp1/css/style.css' %}". This allows you to separate styles inbetween your apps. Please mention that you have to add the appname, here (myapp1) to the static path.

Ok so far so good. Now the next step is to configure your webserver in a way that it serves the staticfiles correctly. Issued url that has to be satisfied is www.yourdomain.com/static/myapp1/css/styles.css. You have to configure your webserver that it leads every request to www.yourdomain.com/static/ to your staticfolder, which is located at <STATIC_ROOT>.

I am kind of sure your projects directory structure is misleading the collectstatic command. Another reason might be the false config of your webserver. Hope it helps.

πŸ‘€Tarquinius

Leave a comment