Carlos
Carlos

Reputation: 6021

File found by Django server but not by Apache/mod_wsgi

I've got a Django app which when run with "manage.py runserver 0.0.0.0:80" works fine, but when run by an Apache2 server tells me "No Such file or Directory"

I reckon it's something to do with telling Apache which path to look in, but I've done the obvious:

WSGIScriptAlias / /home/mywebsite/myproject/wsgi.py
WSGIPythonPath /home/mywebsite
<Directory /home/mywebsite/myapp>
    <Files wsgi.py>
    Order deny,allow
    Require all granted
    </Files>
</Directory>
<Directory /home/mywebsite> //I thought this might give it access to the root folder
    Order deny,allow
    Require all granted
</Directory>

Note my file in the root of the project, next to manage.py. The app is able to display its front page, but the ajax fired by one of the buttons fails due to the above mentioned file error.

I must be missing something about how to specify the file path.

The code to access the file is simply this:

with open('demo.txt') as fp:
   etc...

To reiterate, it works fine when I just run the Django server. Apache runs the main page just fine, it only falls over when looking for this file.

What do I need to change?

Upvotes: 0

Views: 52

Answers (2)

Daniel Backman
Daniel Backman

Reputation: 5241

One way is to set up project path in the settings file: (If it's in the project root)

BASE_DIR = os.path.dirname(os.path.dirname(__file__))

from django.conf import settings
with open(os.path.join(settings.BASE_DIR, 'demo.txt')) as fp:
 ...

It will be consistent across all installations of your project and you can debug BASE_DIR locally.

Upvotes: 1

Guilherme Vierno
Guilherme Vierno

Reputation: 111

Try using the full path of the file, or use the os module to get the relative path.

Upvotes: 1

Related Questions