开发者

Apache not able to load stylesheets and scripts in Django's static directory

开发者 https://www.devze.com 2023-04-02 04:44 出处:网络
I am developing a site using Django. Up until now, when I have used it in a developement environment it was working fine, loading all stylesheets and scripts normally. However, I have currently shifte

I am developing a site using Django. Up until now, when I have used it in a developement environment it was working fine, loading all stylesheets and scripts normally. However, I have currently shifted over to a production environment using Apache Web Server with mod_wsgi for serving Django pages. Now when I am viewing the site, it is reporting that the css and js files couldn't be loaded and were returned with an error code 403 - Access Denied. My web site is the directory "C:\Users\rohitm\Web Applications\WebApp". I have installed Apache 2.2 at "C:\Program Files\Apache Software Foundation\Apache2.2". I have edited the httpd.conf file under the conf sub-directory in the Apache installation folder as follows:

Alias /static/ "C:/Users/开发者_运维问答rohitm/Web Applications/WebApp/static"
<Directory "C:/Users/rohitm/Web Applications/WebApp/static">
Order allow,deny
Allow from all
</Directory>

Alias /static/images/ "C:/Users/rohitm/Web Applications/WebApp/static/images"
<Directory "C:/Users/rohitm/Web Applications/WebApp/static/images">
Order allow,deny
Allow from all
</Directory>

Alias /static/javascript/ "C:/Users/rohitm/Web Applications/WebApp/static/javascript"
<Directory "C:/Users/rohitm/Web Applications/WebApp/static/javascript">
Order allow,deny
Allow from all
</Directory>

Alias /static/stylesheets/ "C:/Users/rohitm/Web Applications/WebApp/static/stylesheets"
<Directory "C:/Users/rohitm/Web Applications/WebApp/static/stylesheets">
Order allow,deny
Allow from all
</Directory>

Alias /static/admin/ "C:/Users/rohitm/Web Applications/WebApp/static/admin"
<Directory "C:/Users/rohitm/Web Applications/WebApp/static/admin">
Order allow,deny
Allow from all
</Directory>

WSGIScriptAlias / "C:/Users/rohitm/Web Applications/WebApp/apache/django.wsgi"
<Directory "C:/Users/rohitm/Web Applications/WebApp/apache">
Order allow,deny
Allow from all
</Directory>

I have created a file django.wsgi in a folder apache under my WebApp folder. The contents are as follows:

import os
import sys
sys.path.append('C:/Users/rohitm/Web Applications/')
sys.path.append('C:/Users/rohitm/Web Applications/WebApp/')
os.environ['DJANGO_SETTINGS_MODULE'] = 'WebApp.settings'

import django.core.handlers.wsgi

application = django.core.handlers.wsgi.WSGIHandler()

The static files settings in settings.py are as follows

STATIC_ROOT = 'C:/Users/rohitm/Web Applications/WebApp/static/'

STATIC_URL = '/static/'

ADMIN_MEDIA_PREFIX = '/static/admin/'

After running the manage.py collectstaic command, the static folder has 4 sub folders - admin, images, stylesheets and javascript. The stylesheet folder contains a file main.css while the javascipt folder contains a file jquery.min.js. The admin folder has all the admin related css and js present there. Now whenever I am viewing the site via http://localhost, the css and javascript files are not loaded, while the browser console (Chrome - Crtl+Shift+J) reports that the files failed to load as the server responded with an error 403 (Forbidden).

Please help urgenty.


Remove the trailing slash on first path given to Alias directive, or alternatively add a trailing slash to second path. The use of trailing slash should be balanced, ie., not on either, or on both.

You also only need the Alias/Directory block for the top directory where static files are. You don't need to enumerate it for all the sub directories. Thus use:

Alias /static "C:/Users/rohitm/Web Applications/WebApp/static"
<Directory "C:/Users/rohitm/Web Applications/WebApp/static">
Order allow,deny
Allow from all
</Directory>

Note now that using '/static' and not '/static/'.


I would guess that the user that Apache is running under does not have access to the C:/Users/rohitm/Web Applications/WebApp/static folder.

In general, it's probably best to put those files somewhere central, rather than in your personal home directory.

0

精彩评论

暂无评论...
验证码 换一张
取 消