user514310
user514310

Reputation:

CSS files are not appearing in production server

My css files are not working in Production server. Ive deployed using WSGI. Will you please fix my problem? Thank you css link

<link href="{{ MEDIA_URL}}css/style.css" rel="stylesheet" type="text/css" />

settings.py

CURRENT_PATH = '/home/nibbler/code/project/
MEDIA_ROOT = os.path.join(CURRENT_PATH, 'templates/media')

MEDIA_URL = '/media/'
TEMPLATE_DIRS = (
    os.path.join(CURRENT_PATH, 'templates/temp_name'),
)

site-available\default

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName project.org
    DocumentRoot "/home/nibbler/code/project/"
    ServerName localhost
    ErrorLog "/home/nibbler/code/project/logs/apache-error.log"
    CustomLog "/home/nibbler/code/project/logs/apache-access.log" common

    Options ExecCGI FollowSymLinks MultiViews

    AddHandler wsgi-script .wsgi
    WSGIDaemonProcess nibbler
    WSGIProcessGroup nibbler

    Alias /media /home/nibbler/code/project/templates/media/
    WSGIScriptAlias / /home/nibbler/code/project/apache/django.wsgi

    DirectoryIndex index.html index.cgi

    AddHandler cgi-script .cgi .pl
</VirtualHost>

urls.py

if settings.DEBUG:
    # static files (images, css, javascript, etc.)
    urlpatterns += patterns('',
        (r'^media/(?P<path>.*)$', 'django.views.static.serve', {
        'document_root': settings.MEDIA_ROOT}))

Upvotes: 0

Views: 356

Answers (2)

Graham Dumpleton
Graham Dumpleton

Reputation: 58523

You have:

Alias /media /home/nibbler/code/project/templates/media/

wrong for a start. Try:

Alias /media/ /home/nibbler/code/project/templates/media/

They must either both have trailing slash or neither have it. You can't have one have it and the other one not have it.

BTW, having:

DocumentRoot "/home/nibbler/code/project/"

is a bad idea. Don't set DocumentRoot to be where your code is. If you were to remove WSGISriptAlias for some reason, all your code would be downloadable by external people.

You are also missing a Directory block with Allow directive in it for where WSGI script files and static files are present. This implies you have outside of this virtual host changed Apache config in some way to say that Apache can serve up files from any directory on your box, which is a bad idea as it strips away one level of security.

Upvotes: 1

Ignas Butėnas
Ignas Butėnas

Reputation: 6307

You have problems only with CSS files or all the media files?

MEDIA_ROOT - should be full path to your files on the system and it seems you have it, so fine. MEDIA_URL - try to put the full URL to the media files. Try to reach them manually with the full url and see if the webserver serves them correctly. If yes, then put the full url to the MEDIA_URL.

Last thing you showed with the if settings.DEBUG is not needed in production. You really want to set DEBUG = False on production server.

By the way Django suggest to have two virtual hosts - one for the Django application itself, another one for the media (where you serve just static content directly with the http server, no dynamic stuff there).

Hope that helped a little...

Upvotes: 0

Related Questions