awsdjango

Get started with Django

To get started, we suggest the following steps:

Step 1. Create a new Django project

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

Step 2. Serve your application through the Apache Web server

For a production environment, it is recommended to configure Apache with the mod_wsgi module before serving your application. For more information, refer to these instructions.

Step 3: Create an HTTPS certificate for Apache

To learn how to create an HTTPS certificate for Apache, refer to the auto-configure a Let’s Encrypt certificate section.

Last modification November 17, 2020