Using static files with the django virtual server
If you're using Django 1.4 then I would use the static tag:
{% load static %}
<link rel="stylesheet" href="{% static "CSS/base.css" %}" type="text/css" />
You probably need this in your urls too, in development files are served via django
if settings.DEBUG:
urlpatterns += staticfiles_urlpatterns() #this serves static files and media files.
#in case media is not served correctly
urlpatterns += patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT,
}),
)
Also in your settings it's common practice to avoid hardcoding locations, here's an example (by the way do you have the static filefinders in your settings?
PROJECT_ROOT = path.dirname(path.abspath(__file__)) #gets directory settings is in
STATIC_ROOT = path.join(PROJECT_ROOT,'static-root')
# this folder is used to collect static files in production. not used in development
STATIC_URL = "/static/"
STATICFILES_DIRS = (
('', path.join(PROJECT_ROOT,'static')), #store site-specific media here.
)
# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
TEMPLATE_CONTEXT_PROCESSORS = (
# other processors...
"django.core.context_processors.static",
)
django static files not appearing with virtual environment
It turns out, that the problem was with my settings.py file.
Specifically, when your still using the django development server with your virtual environment (0.0.0.0:8000), you should have DEBUG set to True, so that it can serve your static files.
Remember! This is only if your using a django development server. For production, it should be set to False.
I got this information from this django article: https://docs.djangoproject.com/en/2.0/howto/static-files/
How to access different static files for different Django applications on IIS?
Found the solution myself when I was messing around with the settings.py
file of my "DjangoApp2". I first considered to add the static files of my DjangoApp2 to the static directory of DjangoApp1, but I thought it could be very inefficient, so I started tweaking some changes in my settings.py file.
So, instead of changing something in the IIS server, I simply changed my STATIC_URL
.
So, first I created a virtual directory for my static files and named it app2_static
. Its physical path was BASE_DIR/static
.
The BASE_DIR is the directory where the manage.py file of
DjangoApp2
is located.
Then, I changed the STATIC_URL
to '/app2_static/'
. Then I restarted the IIS server and the static files were loading on the webpage.
Make sure that your
web.config
file in the static directory of your DjangoApp2 contains only StaticFileModule and no FastCGIModule.
I started a Django project with all requirements but static files can't load successfully
if you do it for your debug server, you can add this lines to your urls.py
from django.conf.urls.static import static
...
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + \
static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Static files served by django in development and apache in production. Why not use same folder?
It's sort of a throwback to the fact that by default, HTTPd structures URLs to mirror the underlying filesystem. It certainly is feasible to use a set of Alias
directives to graft the various static asset subdirectories into the URL structure instead, and I have done just this on a few occasions. It's a bit more work on first deployment, but further revisions to the project require very few if any changes to the configuration, and no extra steps on deployment.
How to fetch static CSS files with django on IIS?
Yes, I am a newbie. Sometimes I ask "newbie questions", same as many others here. But apparently, there is no room for such questions on this site (and this is why I got down-voted on my initial question at the top and got banned out of this site - this is my last answer that I can write on this site). Guys, people ask questions because they honestly ask for help and hope to get it here. They don't do it to irritate any of you (you can vote to close this answer too, if it makes you feel good)
Anyway, after struggling for 2 days, for the sake of those who have the same problem, I wanted to share a step-by-step solution for (probably a common) issue.
Problem
You have started a django project on IIS and it is working perfectly on your localhost. Now, when deploying it to web-server, the static files (CSS, JS, images,..) are not fetched. You can read here and here , but in fact, you don't want all these configurations and copying files from one directory to another...
What you want is that your IIS server will fetch the static files from the static/ directory, just as the development server (the one you run with python manage.py runserver) did.
Solution
1) inside the static/ directory, create a new file called web.config (notice, you probably have another web.config file in upper directory - don;t touch it. just leave it as is).
2) Write the following as the content of this file:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<!-- this configuration overrides the FastCGI handler to let IIS serve the static files -->
<handlers>
<clear/>
<add name="StaticFile" path="*" verb="*" modules="StaticFileModule" resourceType="File" requireAccess="Read" />
</handlers>
</system.webServer>
</configuration>
3) Go to your IIS server -> right click the site name -> Add virtual directory
4) in alias wrote "static" (This is a MUST). Then, navigate to the folder in the project where all the static files are
5) run IIS server and enter your website. It should work.
Django static files with nginx and unicorn
To serve static files do it in nginx level like
server {
server_name 188.xxx.xxx.93;
access_log off;
location ~ ^/(static)/ {
# root:- you static files path
# alias /opt/myapps/uniprogress/static/;
root /opt/myapps/uniprogress/static/;
}
location / {
proxy_pass http://127.0.0.1:8001;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
}
}
Now all static files will serve from your nginx.
How to make Django serve static files with Gunicorn?
When in development mode and when you are using some other server for local development add this to your url.py
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
# ... the rest of your URLconf goes here ...
urlpatterns += staticfiles_urlpatterns()
More info here
When in production you never, ever put gunicorn in front. Instead you use
a server like nginx which dispatches requests to a pool of gunicorn workers and also serves the static files.
See here
Related Topics
Assign CSS Attributes According to Class "Range"
IE9 + Richfaces Rendering Problem
How to Center Block of Inline-Blocks
Highcharts Graph Width Is Incorrect When Scrollbar Is Present, in Bootstrap Fluid Span
How to Collapse Unused Row in a CSS Grid
How to Change Md-Input-Container Placeholder Color Using CSS in Angular Material
Change the Style of an Element If the Fragment Identifier (Hash) in the Url References It
Internet Explorer "CSS Was Ignored Due to Mime Type Mismatch" on Local Files (No Server)
Disable CSS Animation on Pseudo Element Inherited from Parent
Rotating a Text to 270 Degrees in IE8
CSS - Font Being Blocked from Cross-Origin Resource Sharing Policy
CSS Min-Height Not Working on Mozilla Firefox
Background Color of Tspan Element
Selector for Nth Nested Elements
How to Target a Specific Group of Siblings in a Flat Hierarchy
Change Text Color to White on Any Non-White Background