Create Django Web app with PostgreSQL

3 minute read • May 10, 2017

mksunitha
This tutorial describes how to get started running Django on Azure Web Apps with Azure Database for PostgreSQL(Preview). Web Apps provides limited free hosting and rapid deployment, and you can use Python.

Before you begin

    If you don't have an Azure subscription, create a free account before you begin.

    Deploy from Marketplace

    This blog post describes how to get started running a Django app with postgresql from Azure marketplace . This marketplace solution creates the following resources : Web Apps on Windows and Azure Database for PostgreSQL (Preview). Log in to the Azure Portal. Launch the Django + PostgreSQL template in the Azure marketplace to get started. Provide the necessary information for web app and database to be deployed. djangopg1  
    Name Description
    App Name
    Enter a unique app name for your **Web App Name**. This name is used as part of the default DNS name for your app <app_name>.azurewebsites.net, so it needs to be unique across all apps in Azure. You can later map a custom domain name to your app before you expose it to your users
    Subscription Select a Subscription. If you have multiple subscriptions, choose the appropriate subscription.
    Resource group
    Enter a resource group. A resource group is a logical container into which Azure resources like web apps, databases that is deployed and managed. You can create a resource group or use an existing one
    App Service Plan
    App Service plans represent the collection of physical resources used to host your apps. Select the Location and the Pricing tier. For more information on pricing, see App service pricing tier 
    Server Name Enter a postgresql database servername
    Server admin login name Enter a postgresql database administrator username 
    Server admin password Enter a postgresql database administrator password
    Version Azure database for PostgreSQL(Preview) currenlty supports PostgreSQL 9.5 version
    Pricing tier Choose Basic or Standard pricing tier. For more information on pricing, see App service pricing tier 
    Database Name Enter a database name for your web app
    You can watch the progress by clicking the bell icon at the top of the portal page while the app is being deployed. deploy-success When the web app creation is finished, navigate in the Azure portal to the resource group to view the web app and PostgreSQL server. resources-djngo Select the web app line and then click Browse. django-browse

    Develop your application

    The Django + PostgreSQL template contains the Django framework on top of which you can build your application. You can create an Django app using  Kudu. To access Kudu , select your web app in the portal and click Advanced Tools -> Go django-kudu Click on Debug Console  to access the CMD  prompt. In the console run the following commands  under wwwroot folder.
    env\scripts\python.exe env\scripts\django-admin.exe startapp myapp
    console-django This will create myapp folder with starter Django app . For more details in Django app, see Get started with Django . create-app-django

    Database configuration

    You can access the database information within Azure portal by clicking in Application settings -> Connection strings. It is best practice to use App Settings for storing your database information instead of hard coding it in your settings.py file. Create these app settings in Azure portal for your web app. django-appsettings Update DATABASES setting in local settings.py file in your application. to read from App Settings environment variables.
    DATABASES = {
       'default': {
       'ENGINE': 'django.db.backends.postgresql_psycopg2',
       'NAME': os.environ.get('DATABASENAME', ''),
       'USER': os.environ.get('DATABASEUSER', ''),
       'PASSWORD': os.environ.get('DATABASEPASSWORD', ''),
       'HOST': os.environ.get('DATABASEHOST', ''),
       'PORT': '5432',
      }
    }
    

    Database Management

    Use PgAdmin PostgreSQL Client to manage your PostgreSQL server and database remotely.

    Django Poll application sample

    You can deploy your own app via GIT or use this sample Django-poll application. Fork this sample repository or download to locally to start using the sample. For more information to setup GIT, see Local Git Deployment to Azure App Service.

    Next Steps