azuredjango

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 -p /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:

$ cd /opt/bitnami/projects/PROJECT
$ 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:

    $ cd /opt/bitnami/projects/PROJECT
    $ 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:

$ cd /opt/bitnami/projects/PROJECT
$ python manage.py runserver

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

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

Last modification February 9, 2023