12đ
The way you are using staticfiles
is slightly incorrect. While I canât tell you exactly what is causing your current situation, I can tell you that your method will cause you headaches in the future. First things first though, I agree with the comments about watching your request traffic in the Django server terminal. Look for 4xx responses and make sure the requested URL is correct. This: /Static/css/photologue/css
has two errors in it.
If you donât want to read further, drop the urls.py static.server line and watch the server terminal. Now, hereâs how itâs all workingâŠ
Youâve got your settings variables correct but you may misunderstand the purpose of STATIC_ROOT. STATIC_URL is the fully qualified or relative URL for your static files. STATIC_ROOT is the folder that will ultimately hold all the static files. It should be empty. Django is responsible for filling it via the manage.py collectstatic
command. The idea is each app in your Django project has its own static/ folder with the js/css/image assets that it needs. In addition, Django will collect the static assets for the Admin and any other third-party packages you use. All of these assets will be organized into your STATIC_ROOT folder. Itâs not safe to assume files you have their prior to collection will remain.
STATIC_ROOT = '/path/to/empty/static/folder/' # or something dynamic with os.path methods
STATIC_URL = '/static/'
In your case maybe your serenity app has serenity/static/css/serenity.css
and photologue has photologue/static/css/photologue.css
. You could put shared assets in a base/static/
folder.
Now for properly serving static media. Do not use the 'django.views.static.serve'
line in urls.py
. Djangoâs runserver
will automatically serve static files. Behind the scenes it is handling the collectstatic behavior and gathering all your static assets together and serving them up. Using that type of URL pattern in Django 1.3 is unnecessary, a source of confusion, and the kind of thing that will mistakenly go to production. Remember, your webserver (Apache/Nginx) serves static assets. Your Django urls.py files donât need to know a thing about âem.
Referring to the static URL in templates. Youâve got /static/
hardcoded in your templates. That will work (but only because STATIC_URL is the same value). To be more flexible about it, youâve got three options.
- Use
href="{{ STATIC_URL }}css/photologue.css"
. That variable will be in your templates as long as you include âdjango.core.context_processors.staticâ in your TEMPLATE_CONTEXT_PROCESSORS. - Use a templatetag:
{% load static %} ... href="{% get_static_prefix %}css/photologue.css"
- In Django 1.4 youâll be able to use
{% load static from staticfiles %}... href="{% static 'css/photologue.css' %}"
Itâs worth reading up on static files in Django and being aware of the changes coming in Django 1.4
1đ
I see two issues that are likely to happen:
- The STATIC variables were not set in your settings file.
(you had lots of answers for this)
- You have probably not set the /static/ alias in your apache config.
Add this line to your /etc/apache2/httpd.conf file or /etc/apache2/sites-available/myDjangoConfig:
Alias /static/ {valueOfSTATIC_ROOT}
Then restart your Apache server:
sudo service apache2 reload
- [Django]-Understanding python imports
- [Django]-Saving to database in Django tests when using ThreadPoolExecutor
- [Django]-Using Django ORM inside Tornado, "syncdb" doesn't work
0đ
STATIC_ROOT is not used with Django dev server â only when you deploy to prod or use a different server. STATIC_ROOT is where your static files WILL be stored once you collect them (using âcollectstaticâ method).
The reason it works for one app but not the other, is because one app has the right reference to the CSS folder, whereas the other one doesnât. For starters, try copying âstaticâ dir from one app to the other and see if it work.
Much more info here: https://docs.djangoproject.com/en/1.3/howto/static-files/
- [Django]-Does Neomodel support modelform? If so, why isn't it picking up default meta attributes / how do I set them?
- [Django]-Return data in httpresponse for ajax call in Deleteview in django?
- [Django]-Django how to upload folder
0đ
You can try these steps:
- open your settings.py and
-add this at the first line of your file:
import os.path
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
-change your STATIC_ROOTâs value to:
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static/')
-change your STATIC_URLâs value to:
STATIC_URL = '/static/'
- create a folder named âstaticâ in your project root.
- create a folder for your static files like css, javascript and etc. I recommend you use a different folder for different types of files.
-
open the urls.py of your project
-add this to your imports: import settings
-add this to the url patterns:(r'(?:.*?/)?(?P(css|jquery|jscripts|images)/.+)$â, âdjango.views.static.serveâ, {âdocument_rootâ: settings.STATIC_ROOT }),
NOTE: In this example, there are folders named css, jquery, jscripts and images inside my static folder.
-
In your template add this:
for css files: (in this example, default.css is the name of the css file)
<link href="{{ STATIC_URL }}css/default.css" rel="stylesheet" type="text/css" media="all" />
for javascript:
<script type="text/javascript" src="{{ STATIC_URL }}jquery/jquery.js"></script>