Bitnami Django

NOTE: Before running the commands shown on this page, you should load the Bitnami stack environment by executing the installdir/use_APPNAME script (Linux and Mac OS X) or by clicking the shortcut in the Start Menu under "Start -> Bitnami APPNAME Stack -> Application console" (Windows). Learn more.

Django is an open source Web development framework written in Python. It is easy to learn, highly scalable and built for security.

How to create a new superuser?

NOTE: If you are using Django 1.x, you need to replace the python3 command with python.

You can easily create a superuser account to access the Django admin site by following these steps, assuming that the projects folder is located at installdir/apps/django/django_projects.

  • Change to your project folder:

     $ cd installdir/apps/django/django_projects/PROJECT
    
  • Create a new superuser:

     $ python3 manage.py createsuperuser
    
  • Wait for the prompt and fill the required information to set up a new superuser.

How to deploy a Django project?

NOTE: If you are using Django 1.x, you need to replace the python3 command with python.

Development

For development, use the standalone server in Django by executing the following command inside your project folder:

$ python3 manage.py runserver localhost:PORT

localhost and PORT are optional parameters. 127.0.0.1:8000 will be used by default if you don't specify any of them. If you plan to remotely access the server, you should use the IP address 0.0.0.0, or the actual server IP address.

IMPORTANT: Remember to open the port used by the Django server in the server firewall. Refer to the FAQ for more information.

Production

For a production environment, we recommend configuring Apache with the mod_wsgi module before starting to serve your application (already installed and activated by default).

  • First, modify the WSGI application script file at installdir/apps/django/django_projects/PROJECT/PROJECT/wsgi.py. Remember to replace PROJECT with the actual path and name to your Django project.

    TIP: If you are experiencing problems when editing the above file, check if the permissions in your stack are set properly.

    Although the exact content may be different depending on your Django version, ensure that the content is similar to the code below and that you add your project to the path with sys.path.append.

    import os
    import sys
    sys.path.append('installdir/apps/django/django_projects/PROJECT')
    os.environ.setdefault("PYTHON_EGG_CACHE", "installdir/apps/django/django_projects/myproject/egg_cache")
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "PROJECT.settings")
    from django.core.wsgi import get_wsgi_application
    application = get_wsgi_application()
    
  • Create a conf/ folder in the installdir/apps/django/django_projects/PROJECT/ directory.

      $ mkdir installdir/apps/django/django_projects/PROJECT/conf
    
  • Create the following files:

      $ touch installdir/apps/django/django_projects/PROJECT/conf/httpd-prefix.conf
      $ touch installdir/apps/django/django_projects/PROJECT/conf/httpd-app.conf
    
  • On Linux, you can run the application with mod_wsgi in daemon mode. Add the following code in installdir/apps/django/django_projects/PROJECT/conf/httpd-app.conf:

    <IfDefine !IS_DJANGOSTACK_LOADED>
      Define IS_DJANGOSTACK_LOADED
      WSGIDaemonProcess wsgi-djangostack   processes=2 threads=15    display-name=%{GROUP}
    </IfDefine>
    
    <Directory "installdir/apps/django/django_projects/PROJECT/PROJECT">
        Options +MultiViews
        AllowOverride All
        <IfVersion >= 2.3>
            Require all granted
        </IfVersion>
    
        WSGIProcessGroup wsgi-djangostack
    
        WSGIApplicationGroup %{GLOBAL}
    </Directory>
    
    Alias /PROJECT/static "installdir/apps/django/lib/python3.6/site-packages/Django-2.0.2-py3.6.egg/django/contrib/admin/static"
    WSGIScriptAlias /PROJECT 'installdir/apps/django/django_projects/PROJECT/PROJECT/wsgi.py'
    
  • In the installdir/apps/django/django_projects/PROJECT/conf/httpd-prefix.conf file, add this code:

    Include "installdir/apps/django/django_projects/PROJECT/conf/httpd-app.conf"
    
  • Add the line below to the installdir/apache2/conf/bitnami/bitnami-apps-prefix.conf file:

    Include "installdir/apps/django/django_projects/PROJECT/conf/httpd-prefix.conf"
    
  • Edit the installdir/apps/django/django_projects/myproject/myproject/settings.pyfile and update the ALLOWED_HOSTS variable with the IP address of your server, as in the example below:

    ALLOWED_HOSTS = ['localhost', 'localhost', '127.0.0.1']
    
  • Restart the Apache server:

    $ sudo installdir/ctlscript.sh restart apache
    

You should now be able to browse to your project at http://localhost/PROJECT.

How to configure a database for your project?

If you wish to configure a database for your project, configure the settings.py file with the following settings. The examples below show how to configure the database connection with a database called "djangostack".

MySQL

DATABASES = {
  'default': {
      'ENGINE': 'django.db.backends.mysql',
      'NAME': 'djangostack',
      'HOST': 'installdir/mysql/tmp/mysql.sock',
      'PORT': '3306',
      'USER': 'USERNAME',
      'PASSWORD': 'PASSWORD'
  }
}

PostgreSQL

DATABASES = {
  'default': {
      'ENGINE': 'django.db.backends.postgresql_psycopg2',
      'NAME': 'djangostack',
      'HOST': 'installdir/postgresql',
      'PORT': '5432',
      'USER': 'USERNAME',
      'PASSWORD': 'PASSWORD'
  }
}

SQLite

DATABASES = {
  'default': {
      'ENGINE': 'django.db.backends.sqlite3',
      'NAME': 'installdir/apps/django/django_projects/PROJECT/PROJECT/PROJECT.db'
  }
}

How to create a new Django project?

NOTE: If you are using Django 1.x, you need to replace the python3 command with python.

The location of the Django projects folder depends on the platform. The projects folder is located at installdir/apps/django/django_projects (on Linux and Mac OS X), at C:\Documents and Settings\USER\Bitnami DjangoStack Projects (on Windows 2000, Windows XP and Windows 2003 Server) and at C:\Users\USER\Bitnami DjangoStack Projects (on Windows Vista, Windows 7 and Windows 2008 Server).

Follow the steps below:

  • Change to your projects folder and run the following command to create a new project. Replace the PROJECT placeholder with the name of your project in this and all subsequent commands.

     $ django-admin.py startproject PROJECT
    
  • If you wish to create a new application inside the project, execute the following commands as well. Replace the APP placeholder with the name of your application in this and all subsequent commands.

     $ cd PROJECT
     $ python3 manage.py startapp APP
    
  • Edit the installdir/apps/django/django_projects/PROJECT/APP/views.py file and add this content:

     from django.http import HttpResponse
    
     def index(request):
       return HttpResponse("Hello world!")
    
  • Create the installdir/apps/django/django_projects/PROJECT/APP/urls.py file and add these lines to it:

     from django.conf.urls import url
    
     from . import views
    
     urlpatterns = [
       url(r'^$', views.index, name='index'),
     ]
    
  • Edit the installdir/apps/django/django_projects/PROJECT/PROJECT/urls.py file and modify it to look like this:

     from django.conf.urls import url
     from django.urls import include
    
     urlpatterns = [
         url(r'^APP/', include('APP.urls')),
     ]
    
  • Follow the steps to deploy your Django project with Apache.

You should now be able to see your new application at http://localhost/PROJECT/APP.

If you prefer to use the standalone server in Django, refer to this section. In this case, you should be able to see your new application at http://localhost:8000/APP.

To get started with Django, check the official Django documentation for the version that you are using.

NOTE: When running the commands shown on this page, replace the installdir placeholder with the full installation directory for your Bitnami stack.

How to upgrade Django's Python package?

NOTE: If you are using Django 1.x, you need to replace the python3 command with python.
NOTE: Before upgrading Django, make sure that your Django applications are compatible with the new version.

Follow the steps below:

  • Open the Bitnami Console and load the Bitnami environment.

  • Execute this command from the uncompressed directory:

     $ sudo pip install --upgrade Django --install-option="--prefix=installdir/apps/django" (Linux and Mac OS X)
    
     > pip install --upgrade Django --install-option="--prefix=installdir\apps\django --install-lib=installdir\apps\django --install-scripts=installdir\apps\django\django\bin --install-data=installdir\apps\django" (Windows)
    

    If you want a specific version of Django (not the latest one), add the version number as shown below:

     $ sudo pip install --upgrade Django==1.6.5 --install-option="--prefix=installdir/apps/django" (Linux and Mac OS X)
    
     > pip install --upgrade Django==1.6.5 --install-option="--prefix=installdir\apps\django --install-lib=installdir\apps\django --install-scripts=installdir\apps\django\django\bin --install-data=installdir\apps\django" (Windows)        
    
  • Edit the django-admin.py script (usually in installdir/apps/django/bin) and change the first line to use python3 instead of .python2.7.bin:

     #!installdir/python/bin/python3
    

How to configure Django with Aptana or Eclipse?

Bitnami Stacks are self-contained and independent of your system. If you want to configure the Aptana development tool, load the stack environment before using it.

$ source installdir/scripts/setenv.sh
$ /opt/Aptana_Studio_3/AptanaStudio3

You can also integrate with Eclipse:

$ source installdir/scripts/setenv.sh
$ eclipse

Find a quick guide (Spanish) at http://pategon.blogspot.com.es/2012/09/bitnami-djangostack-eclipse-pydev-aptana.html.

nativeInstaller

Bitnami Documentation