Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starting first Django project errors

My computer runs Ubuntu 12.04 and I followed this tutorial to get started with Django: http://blog.stannard.net.au/2010/12/11/installing-django-with-apache-and-mod_wsgi-on-ubuntu-10-04/

I tried to start in /srv/www/ my alex project by running this command sudo django-admin.py startproject alex

Then created apache folder and django.wsgi file (/srv/www/alex/apache/django.wsgi)

tree for /srv/www/alex/

.
├── apache
│   └── django.wsgi
├── alex
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── manage.py

2 directories, 6 files

alex apache site:

<VirtualHost *:80>

    ServerName alex.djangoserver
    DocumentRoot /srv/www/alex

    <Directory /srv/www/alex>
        Order allow,deny
        Allow from all
    </Directory>

    WSGIDaemonProcess alex.djangoserver processes=2 threads=15 display-name=%{GROUP}
    WSGIProcessGroup alex.djangoserver

    WSGIScriptAlias / /srv/www/alex/apache/django.wsgi

</VirtualHost>

The apache error log result:

[error] [client 127.0.0.1] mod_wsgi (pid=28076): Exception occurred processing WSGI script '/srv/www/floqq/alex/django.wsgi'.
[error] [client 127.0.0.1] Traceback (most recent call last):
[error] [client 127.0.0.1]   File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 219, in __call__
[error] [client 127.0.0.1]     self.load_middleware()
[error] [client 127.0.0.1]   File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 39, in load_middleware
[error] [client 127.0.0.1]     for middleware_path in settings.MIDDLEWARE_CLASSES:
[error] [client 127.0.0.1]   File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 184, in inner
[error] [client 127.0.0.1]     self._setup()
[error] [client 127.0.0.1]   File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 42, in _setup
[error] [client 127.0.0.1]     self._wrapped = Settings(settings_module)
[error] [client 127.0.0.1]   File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 95, in __init__
[error] [client 127.0.0.1]     raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
[error] [client 127.0.0.1] ImportError: Could not import settings 'alex.settings' (Is it on sys.path?): No module named alex.settings

UPDATE

apache/django.wsgi

import os
import sys

path = '/srv/www'
if path not in sys.path:
    sys.path.insert(0, '/srv/www')

os.environ['DJANGO_SETTINGS_MODULE'] = 'alex.settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
like image 226
Alex Avatar asked Aug 25 '26 19:08

Alex


2 Answers

In your wsgi file you have:

sys.path.insert(0, '/srv/www')

os.environ['DJANGO_SETTINGS_MODULE'] = 'alex.settings'

which will search for settings.py in /srv/www/alex/.

That fails because, according to your posted tree, settings.py is in /srv/www/alex/alex/.

So you have 3 options:

  1. change the inserted path
  2. change the directory sturcture, or
  3. refactor /srv/www/alex/ into a package containing alex.settings.
like image 118
ch3ka Avatar answered Aug 27 '26 09:08

ch3ka


You shouldn't use sudo with any commands that you need during development; because you don't do development as root and it will lead to other problems down the road with permissions.

If you are new to django one of the first things that's different about it than more traditional development frameworks (like PHP) is that you don't need a web server or database server to get started. Django comes with everything you need during development, including a web server which you start with the runserver command.

You should also use the official tutorial to get you started. Once you are comfortable with the tutorial then you will have a better understanding of how to deploy projects and can follow other posts easily.

Apache and mod_wsgi is best when you are deploying projects, and not optimal (or necessary) during development.

I suggest you start with the following steps:

  • First, you should execute sudo apt-get install python-virtualenv, which will allow you to create a separate environment for your python/django work. This is the only command you should run as root, and it will only be done once.

  • Now, to get started. Open a terminal and type in the following as your normal user:

    $ virtualenv --no-site-packages django_project

    This will create a new environment which you can use for your first project. Once that command has executed, type this:

    $ source django_project/bin/activate

    This activates the new virtual environment. You'll notice your prompt now has (django_project) listed. This lets you know you are in the virtual environment. Finally, you should install django:

    (django_project) $ pip install django

Now that django is installed and you can start with the tutorial. In case you close your terminal (or open a new tab or terminal window), you can reactivate the virtual environment by executing source django_project/bin/activate. You can get back to your normal shell by typing deactivate in the virtual environment.

Hope that helps.

like image 30
Burhan Khalid Avatar answered Aug 27 '26 09:08

Burhan Khalid