Django

    - GeoDjango with PostGIS quick start guide

    How to create a new Django project?

    Cloud Images

    The projects folder is located at /opt/bitnami/apps/django/django_projects.

    Follow the steps below:

    1. 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.   

    $ sudo django-admin.py startproject PROJECT
    

    2. 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
    $ sudo python manage.py startapp APP
    

    You can check your Django version at any time by executing the following command:

    python -c "import django; print(django.get_version())"
    

    3. Edit the /opt/bitnami/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!")
    

    4. Create the /opt/bitnami/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'),
    ]
    

    5. Edit the /opt/bitnami/apps/django/django_projects/PROJECT/PROJECT/urls.py file and add this line to the list of URL patterns.

    url(r'^APP/', include('APP.urls')),
    

    For example, your file should look like this:

    url(r'^admin/', include(admin.site.urls)),
    url(r'^APP/', include('APP.urls')),
    url(r'^$', default_urlconf),
    

    6. Restart Apache.

    $ sudo /opt/bitnami/ctlscript.sh restart apache
    

    You should now be able to see your new application at http://[server-IP-address]/PROJECT/APP.

    For getting started with Django, check the official Django documentation for the version that you are using.

    Virtual Machine

    The projects folder is located at /opt/bitnami/apps/django/django_projects.

    Follow the steps below:

    1. 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.   

    $ sudo django-admin.py startproject PROJECT
    

    2. 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
    $ sudo python manage.py startapp APP
    

    You can check your Django version at any time by executing the following command:

    python -c "import django; print(django.get_version())"
    

    3. Edit the /opt/bitnami/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!")
    

    4. Create the /opt/bitnami/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'),
    ]
    

    5. Edit the /opt/bitnami/apps/django/django_projects/PROJECT/PROJECT/urls.py file and add this line to the list of URL patterns.

    url(r'^APP/', include('APP.urls')),
    

    For example, your file should look like this:

    url(r'^admin/', include(admin.site.urls)),
    url(r'^APP/', include('APP.urls')),
    url(r'^$', default_urlconf),
    

    6. Restart Apache.

    $ sudo /opt/bitnami/ctlscript.sh restart apache
    

    You should now be able to see your new application at http://[server-IP-address]/PROJECT/APP.

    For getting started with Django, check the official Django documentation for the version that you are using.

    Native Installer

    The location of the Django projects folder depends on the platform, as shown below:

    On Linux:

    The projects folder is located at installdir/apps/django/django_projects.

    On Windows:

    • For 2000, XP and 2003 Server, the projects folder is located at C:\Documents and Settings\USER\Bitnami DjangoStack Projects.
    • For Vista, 7 and 2008 Server, the projects folder is located at C:\Users\USER\Bitnami DjangoStack Projects.

    On Mac OS X:

    The projects folder is located at /Applications/djangostack-VERSION/apps/django/django_projects.

    To start a project with Django, follow these steps:

    1. Start the Bitnami Console, by running the command below using the console.

    $ cd ~/djangostack-VERSION
    $ ./use_django
    

    2. 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 

    3. 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 
    $ python manage.py startapp APP
    

    You can check your Django version at any time by executing the following command:

    python -c "import django; print(django.get_version())"
    

    4. 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!")
    

    5. 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'),
    ]
    

    6. Edit the installdir/apps/django/django_projects/PROJECT/PROJECT/urls.py file and add this line to the list of URL patterns.

    url(r'^APP/', include('APP.urls')),
    

    For example, your file should look like this:

    url(r'^admin/', include(admin.site.urls)),
    url(r'^APP/', include('APP.urls')),
    url(r'^$', default_urlconf),
    

    7. Restart Apache.

    $ installdir/ctlscript.sh restart apache
    

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

    For getting started with Django, check the official Django documentation for the version that you are using.

    How to start a Django project?

    Follow the steps below to develop and deploy your Django project.

    Development

    Cloud Images

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

    $ python manage.py runserver IP:PORT
    

    IP 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 your cloud platform's page in this wiki for more information.

    Virtual Machine

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

    $ python manage.py runserver IP:PORT
    

    IP 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 these instructions for more information.

    Native Installer

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

    $ python manage.py runserver IP:PORT
    

    IP 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 server's network IP address.

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

    Tabs end

    Production deployment

    For a production environment, we recomend configuring Apache with the mod_wsgi module before starting to serve your application. On Bitnami Cloud Images and Virtual Appliances, mod_wsgi is already installed and activated by default.

    Cloud Images

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

    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('/opt/bitnami/apps/django/django_projects/PROJECT')
    os.environ.setdefault("PYTHON_EGG_CACHE", "/opt/bitnami/apps/django/django_projects/PROJECT/egg_cache")
    
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "PROJECT.settings")
    
    from django.core.wsgi import get_wsgi_application
    application = get_wsgi_application()
    
     
    2. Create a conf/ folder in the  /opt/bitnami/apps/django/django_projects/PROJECT/ directory. 
    $ sudo mkdir /opt/bitnami/apps/django/django_projects/PROJECT/conf
    

    3. Create the following files:

    $ sudo touch /opt/bitnami/apps/django/django_projects/PROJECT/conf/httpd-prefix.conf
    $ sudo touch /opt/bitnami/apps/django/django_projects/PROJECT/conf/httpd-app.conf
    
     
    4. On Linux, you can run the application with mod_wsgi in daemon mode. Add the following code in /opt/bitnami/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>
    
    WSGIScriptAlias /PROJECT '/opt/bitnami/apps/django/django_projects/PROJECT/PROJECT/wsgi.py'
    <Directory "/opt/bitnami/apps/django/django_projects/PROJECT/PROJECT/">
        WSGIProcessGroup wsgi-djangostack
        WSGIApplicationGroup %{GLOBAL}
        <IfVersion < 2.3 >
            Order allow,deny
            Allow from all
        </IfVersion>
        <IfVersion >= 2.3>
            Require all granted
        </IfVersion>
    </Directory>
    

    5. In the /opt/bitnami/apps/django/django_projects/PROJECT/conf/httpd-prefix.conf file, add this code:

    Include "/opt/bitnami/apps/django/django_projects/PROJECT/conf/httpd-app.conf"
    

    6. Add the line below to the /opt/bitnami/apache2/conf/bitnami/bitnami-apps-prefix.conf file:

    Include "/opt/bitnami/apps/django/django_projects/PROJECT/conf/httpd-prefix.conf"
    

    7. Restart the Apache server.

    $ sudo /opt/bitnami/ctlscript.sh restart apache
    

    You should now be able to browse to your project at http://[server-IP-address]/PROJECT.

    Virtual Machine

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

    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('/opt/bitnami/apps/django/django_projects/PROJECT')
    os.environ.setdefault("PYTHON_EGG_CACHE", "/opt/bitnami/apps/django/django_projects/PROJECT/egg_cache")
    
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "PROJECT.settings")
    
    from django.core.wsgi import get_wsgi_application
    application = get_wsgi_application()
    
     
    2. Create a conf/ folder in the  /opt/bitnami/apps/django/django_projects/PROJECT/ directory. 
    $ sudo mkdir /opt/bitnami/apps/django/django_projects/PROJECT/conf
    

    3. Create the following files:

    $ sudo touch /opt/bitnami/apps/django/django_projects/PROJECT/conf/httpd-prefix.conf
    $ sudo touch /opt/bitnami/apps/django/django_projects/PROJECT/conf/httpd-app.conf
    
     
    4. On Linux, you can run the application with mod_wsgi in daemon mode. Add the following code in /opt/bitnami/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>
    
    WSGIScriptAlias /PROJECT '/opt/bitnami/apps/django/django_projects/PROJECT/PROJECT/wsgi.py'
    <Directory "/opt/bitnami/apps/django/django_projects/PROJECT/PROJECT/">
        WSGIProcessGroup wsgi-djangostack
        WSGIApplicationGroup %{GLOBAL}
        <IfVersion < 2.3 >
            Order allow,deny
            Allow from all
        </IfVersion>
        <IfVersion >= 2.3>
            Require all granted
        </IfVersion>
    </Directory>
    

    5. In the /opt/bitnami/apps/django/django_projects/PROJECT/conf/httpd-prefix.conf file, add this code:

    Include "/opt/bitnami/apps/django/django_projects/PROJECT/conf/httpd-app.conf"
    

    6. Add the line below to the /opt/bitnami/apache2/conf/bitnami/bitnami-apps-prefix.conf file:

    Include "/opt/bitnami/apps/django/django_projects/PROJECT/conf/httpd-prefix.conf"
    

    7. Restart the Apache server.

    $ sudo /opt/bitnami/ctlscript.sh restart apache
    

    You should now be able to browse to your project at http://[server-IP-address]/PROJECT.

    Native Installer

    1. 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.

    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/PROJECT/egg_cache")
    
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "PROJECT.settings")
    
    from django.core.wsgi import get_wsgi_application
    application = get_wsgi_application()
    
     
    2. Create a conf/ folder in the installdir/apps/django/django_projects/PROJECT/ directory. 
    $ mkdir installdir/apps/django/django_projects/PROJECT/conf
    

    3. 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
    
     
    4. On Linux and Mac OS X, 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>
    
    WSGIScriptAlias /PROJECT 'installdir/apps/django/django_projects/PROJECT/PROJECT/wsgi.py'
    <Directory "installdir/apps/django/django_projects/PROJECT/PROJECT/">
        WSGIProcessGroup wsgi-djangostack
        WSGIApplicationGroup %{GLOBAL}
        <IfVersion < 2.3 >
            Order allow,deny
            Allow from all
        </IfVersion>
        <IfVersion >= 2.3>
            Require all granted
        </IfVersion>
    </Directory>
    
     
    On Windows, the daemon mode for mod_wsgi is not support and the configuration will be slightly different:
    WSGIScriptAlias /PROJECT 'installdir/apps/django/django_projects/PROJECT/PROJECT/wsgi.py'
    <Directory "installdir/apps/django/django_projects/PROJECT/PROJECT/">
        WSGIApplicationGroup %{GLOBAL}
        <IfVersion < 2.3 >
            Order allow,deny
            Allow from all
        </IfVersion>
        <IfVersion >= 2.3>
            Require all granted
        </IfVersion>
    </Directory>
    

    5. 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"
    

    6. 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"
    

    7. Restart the Apache server.

    $ installdir/ctlscript.sh restart apache
    

    You should now be able to browse to your project at http://[server-IP-address]/PROJECT (Windows) or http://[server-IP-address]/PROJECT (Linux and Mac OS X).

    Tabs end

    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". Remember to replace /opt/bitnami with the actual path to your stack installation directory for native installers.

    For MySQL:

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

    For PostgreSQL:

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2',
            'NAME': 'djangostack',
            'HOST': '/opt/bitnami/postgresql',
            'PORT': '5432',
            'USER': 'USERNAME',
            'PASSWORD': 'PASSWORD'
        }
    }
    
    NOTE: The database connection in the above configuration file is configured to use a socket. If you are using the Bitnami Django Stack on Windows, you should instead replace the HOST parameter with 'HOST': '127.0.0.1'.

    For SQLite:

     DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': '/opt/bitnami/apps/django/django_projects/Project/Project/Project.db',
        }
    }
     

    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, you can 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
    

    You can find a quick guide (spanish) at http://pategon.blogspot.com.es/2012/09/bitnami-djangostack-eclipse-pydev-aptana.html

    How to upgrade Django python package?

    Before upgrading Django make sure that your Django applications are compatible with the new version.

    Open the BitNami Console to load the BitNami environment if you are using the Native Installer. If you are using the Virtual Machine or the Cloud Image is not necessay.

    On Windows execute:

    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"
    

    On virtual machines and cloud images execute from the uncompressed directory:

    sudo pip install --upgrade Django --install-option="--prefix=/opt/bitnami/apps/django"
    

    On Linux and Mac OS X execute from the uncompressed directory:

    pip install --upgrade Django --install-option="--prefix=/opt/bitnami/apps/django"
    

    If you want a specific version of Django (not the latest one) you can specify Django==<version> instead (example: Django==1.6.5)

    Edit the django-admin.py script (that you can find in apps/django/bin) and change the first line to use python instead of .python2.7.bin:

    #!/opt/bitnami/python/bin/.python2.7.bin
    

    with

    #!/opt/bitnami/python/bin/python
    
    Tag page (Edit tags)
    • No tags
    Pages that link here
    Page statistics
    76727 view(s), 52 edit(s) and 26565 character(s)

    Comments

    You must login to post a comment.

    Attach file

    Attachments