Read a local file in django

For this use, I'd put it in the settings module. In settings.py, add e.g. MY_LONG_QUERY = 'from FOO select BAR...'. Then, in your view just load it from the settings like so:

from django.conf import settings
settings.MY_LONG_QUERY

But, this doesn't really answer your question. Assuming permissions and all are correct, keep a reference to your project root in your settings like this:

ROOT_PATH = os.path.split(os.path.abspath(__file__))[0]

And then again in your view, open your file like so:

from django.conf import settings

def read_from_database(request):
    f = open(os.path.join(settings.ROOT_PATH, 'myfile.db'))
    # Do something with f

Keep the file in django project root and add the following in the settings.py file.

PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))

Then in the view do this.

import os
from django.conf.settings import PROJECT_ROOT

file_ = open(os.path.join(PROJECT_ROOT, 'filename'))

Update:

In newer Django versions BASE_DIR is already defined in the settings.py file. So you can do the following.

import os
from django.conf import settings

file_ = open(os.path.join(settings.BASE_DIR, 'filename'))