azuredjango

Create a new Django project

NOTE: We are in the process of modifying the file structure and configuration for many Bitnami stacks. On account of these changes, the file paths stated in this guide may change depending on whether your Bitnami stack uses native Linux system packages (Approach A), or if it is a self-contained installation (Approach B). To identify your Bitnami installation type and what approach to follow, run the command below:

 $ test ! -f "/opt/bitnami/common/bin/openssl" && echo "Approach A: Using system packages." || echo "Approach B: Self-contained installation."

The output of the command indicates which approach (A or B) is used by the installation, and will allow you to identify the paths, configuration and commands to use in this guide. Refer to the FAQ for more information on these changes.

Approach A: Bitnami installations using system packages

  • First, create a new folder to store your Django projects, such as the /opt/bitnami/projects directory, and give write permissions for the current system user. Replace the PROJECT placeholder with the name of your project in this and all subsequent commands.

    $ sudo mkdir /opt/bitnami/projects/PROJECT
    $ sudo chown -R $USER /opt/bitnami/projects
    
  • Then, initialize a new Django project with the following command:

    $ django-admin startproject PROJECT /opt/bitnami/projects/PROJECT
    

Configure database credentials

If you wish to configure a database for your project, you will need to configure the database credentials in the settings.py file. The examples below show how to configure the database connection with a database called “PROJECT”.

NOTE: Replace the USERNAME and PASSWORD placeholders with your database user and password, respectively. Learn how to obtain the database credentials.

  • SQLite:

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
        }
    }
    
  • MariaDB:

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

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

Create project database and initialize the schema

To create the project database and initialize the schema for your Django project, change to your project directory and run the following command:

$ python manage.py migrate

Add a sample application with a welcome page

By default, Django does not add any application to your project. To add an application, follow the steps below:

  • Create the helloworld application:

    $ python manage.py startapp helloworld
    
  • Edit the helloworld/views.py file and ensure it contains the following:

    from django.http import HttpResponse
    def index(request):
        return HttpResponse("Hello, world.")
    
  • Create and edit the helloworld/urls.py file and ensure it contains the following:

    from django.urls import path
    from . import views
    urlpatterns = [
        path('', views.index, name='index'),
    ]
    
  • Edit the PROJECT/urls.py file and ensure it contains the following:

    from django.contrib import admin
    from django.urls import include, path
    urlpatterns = [
        path('', include('helloworld.urls')),
        path('admin/', admin.site.urls),
    ]
    

Test your Django project

The Django project can be started by using this command from the /opt/bitnami/projects/PROJECT directory, and it will run on port 8000:

$ python manage.py runserver

To access the application, browse to http://SERVER-IP:8000/. To end the application, terminate the running Django process.

Approach B: Self-contained Bitnami installations

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

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.

    $ cd /opt/bitnami/apps/django/django_projects/PROJECT
    $ 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 /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!")
    
  • 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'),
    ]
    
  • Edit the /opt/bitnami/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')),
    ]
    

    If you have Django version 1.x use:

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

    • Option 1: To use the standalone server in Django, refer to the following section. Note that in this case, you will need to create an SSH tunnel to port 8000 (or the port configured for the standalone server) following these instructions. In this case, you should be able to see your new application at http://localhost:8000/APP.

    • Option 2: To use Apache to deploy your project, refer to the following section. In this case, you should be able to see your new application at http://SERVER-IP/PROJECT/APP.

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

Last modification November 17, 2020