title: How to correctly read local text file content in django?
body: <p>Good day, I'm receiving the following error, while trying to read from  a file: </p>
  
  <pre><code>Exception Type: KeyError
  Exception Value:    
  'opened'
  </code></pre>
  
  <p>The error is coming from the bellow line in readFile: </p>
  
  <pre><code>if fileHandler['opened']:
  </code></pre>
  
  <p>and this is how my view looks like:
      from project.settings import text_file
      from django.core.files import File
      from django.shortcuts import render</p>
  
  <pre><code>def home_view(request):
      context = {'error': ''}
      readFile(context)
      render(request, 'index.html', context)
  
  
  def readFile(context):
      fileHandler = open_file(context, 'r')
  
      if fileHandler['opened']:
          file = File(fileHandler['handler'])
          read_content(file, context)
  
          file.close()
  
  
  def open_file(context, mode):
      try:
          fileHandler = open(text_file, mode)
          return {'open': True, 'handler': fileHandler}
  
      except IOError:
          context['error'] += 'Unable to open file.\n'
      except:
          context['error'] += 'Unexpected exception in openFile method.\n'
          return {'opened':False, 'handler': None}
  
  
  def read_content(file, context):
      context['fileContent'] = ''
      for sentence in file.chunks(10):
          context['fileContent'] += sentence
  </code></pre>
  
  <p>and in me settings:</p>
  
  <pre><code>BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  text_file = os.path.join(BASE_DIR, 'my_file.txt')
  </code></pre>
  
  <p>Will really appreaciateany help.</p>