{"id":1176,"date":"2022-03-02T12:56:30","date_gmt":"2022-03-02T20:56:30","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/azure-sql\/?p=1176"},"modified":"2022-03-02T12:56:30","modified_gmt":"2022-03-02T20:56:30","slug":"create-rest-api-in-python-with-django-using-the-django-rest-framework-and-azure-sql","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/azure-sql\/create-rest-api-in-python-with-django-using-the-django-rest-framework-and-azure-sql\/","title":{"rendered":"Create REST API in Python with Django, using the Django REST Framework and Azure SQL"},"content":{"rendered":"<p>Django is a Python-based open-source web framework. It is a popular and well-liked web framework among developers all around the world. But wouldn&#8217;t it be amazing to build a website with Django while also taking advantage of Azure SQL database&#8217;s security, performance, high availability, and other <a href=\"https:\/\/techcommunity.microsoft.com\/t5\/azure-sql-blog\/10-reasons-why-azure-sql-is-the-best-database-for-developers\/ba-p\/969055\">great capabilities<\/a>?<\/p>\n<p>In this article, we will create a REST API in Python with Django, using the Django REST Framework and Azure SQL database that allows you to perform CRUD operations. Along the way, I will also show you how you can deploy your Django-based app on Azure app service.<\/p>\n<div><div class=\"alert alert-primary\"><p class=\"alert-divider\"><i class=\"fabric-icon fabric-icon--Info\"><\/i><strong>Note<\/strong><\/p><a href=\"https:\/\/github.com\/microsoft\/mssql-django\">mssql-django<\/a> v1.1 is used to establish database connectivity with Azure SQL. mssql-django is a fork of <a href=\"https:\/\/pypi.org\/project\/django-mssql-backend\/\">django-mssql-backend<\/a>. This driver provides an enterprise database connectivity option for the Django Web Framework, with support for Microsoft SQL Server and Azure SQL Database. mssql-django supports Django 2.2, 3.0, 3.1, 3.2 and 4.0.<\/div><\/div>\n<hr \/>\n<div>\n<h2>Download the sample code<\/h2>\n<p dir=\"auto\">Clone this repository:<\/p>\n<div class=\"highlight highlight-source-shell position-relative overflow-auto\">\n<pre>git clone https:\/\/github.com\/azure-samples\/azure-sql-db-django<\/pre>\n<\/div>\n<p dir=\"auto\">Alternatively, you can clone the code using Visual Studio Code as well.<\/p>\n<ul>\n<li>Open the folder location where you want to clone the code<\/li>\n<li>In Visual Studio Code, select Source Control &gt; &#8230; &gt; Clone (or select View, Command Palette and enter Git:Clone), paste the\u00a0<a href=\"https:\/\/github.com\/azure-samples\/azure-sql-db-django.git\">Git repository URL<\/a>, and then select Enter&lt;\/&gt;.<\/li>\n<\/ul>\n<p dir=\"auto\">Once you have the code downloaded to your local computer. You should see folder structure as below:<\/p>\n<\/div>\n<div>\n<pre>azure-sql-db-django\r\n \u2523 customerapi\r\n \u2503 \u2523 migrations\r\n \u2503 \u2523 admin.py\r\n \u2503 \u2523 apps.py\r\n \u2503 \u2523 models.py\r\n \u2503 \u2523 serializers.py\r\n \u2503 \u2523 tests.py\r\n \u2503 \u2523 urls.py\r\n \u2503 \u2523 views.py\r\n \u2503 \u2517 __init__.py\r\n \u2523 django-sql-project\r\n \u2503 \u2523 asgi.py\r\n \u2503 \u2523 settings.py\r\n \u2503 \u2523 urls.py\r\n \u2503 \u2523 wsgi.py\r\n \u2503 \u2517 __init__.py\r\n \u2523 LICENSE\r\n \u2523 manage.py\r\n \u2523 README.md\r\n \u2517 requirements.txt<\/pre>\n<\/div>\n<div><\/div>\n<div>\n<h2>Create Azure SQL Database<\/h2>\n<p dir=\"auto\">If you don&#8217;t have an Azure SQL server already, you can create one (no additional costs for a server) by running the following\u00a0<a href=\"https:\/\/docs.microsoft.com\/en-us\/cli\/azure\/\" rel=\"nofollow\">AZ CLI<\/a>\u00a0command (via\u00a0<a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/wsl\/\" rel=\"nofollow\">WSL<\/a>, or Linux or\u00a0<a href=\"https:\/\/azure.microsoft.com\/en-us\/features\/cloud-shell\/\" rel=\"nofollow\">Azure Cloud Shell<\/a>):<\/p>\n<p dir=\"auto\">Create a resource group if you don&#8217;t have one already created:<\/p>\n<div class=\"snippet-clipboard-content position-relative overflow-auto\">\n<pre><code>az group create -l &lt;location&gt; -n &lt;MyResourceGroup&gt;\r\n<\/code><\/pre>\n<\/div>\n<p dir=\"auto\">Create the Database Server:<\/p>\n<div class=\"snippet-clipboard-content position-relative overflow-auto\">\n<pre><code>az sql server create -n &lt;server-name&gt; -l &lt;location&gt; --admin-user &lt;admin-user&gt; --admin-password &lt;admin-password&gt; -g &lt;resource-group&gt;\r\n<\/code><\/pre>\n<\/div>\n<blockquote>\n<p dir=\"auto\"><strong>Note:<\/strong> Make sure to note the database name, username and password somewhere safe.<\/p>\n<\/blockquote>\n<p dir=\"auto\">Create a new Azure SQL database:<\/p>\n<div class=\"snippet-clipboard-content position-relative overflow-auto\">\n<pre><code>az sql db create -g &lt;resource-group&gt; -s &lt;server-name&gt; -n my-db --service-objective GP_Gen5_2\r\n<\/code><\/pre>\n<\/div>\n<p dir=\"auto\">Make sure you have the firewall configured to allow your machine to access Azure SQL:<\/p>\n<div class=\"snippet-clipboard-content position-relative overflow-auto\">\n<pre><code>az sql server firewall-rule create --resource-group &lt;resource-group&gt; --server &lt;server-name&gt; --name AllowMyClientIP_1 --start-ip-address &lt;your_public_ip&gt; --end-ip-address &lt;your_public_ip&gt;\r\n<\/code><\/pre>\n<\/div>\n<p dir=\"auto\">You can get your public IP\u00a0<a href=\"https:\/\/ipinfo.io\/ip\" rel=\"nofollow\">here<\/a>\u00a0or through other ways, for example:\u00a0<a href=\"https:\/\/ifconfig.me\/\" rel=\"nofollow\">https:\/\/ifconfig.me\/<\/a><\/p>\n<h2>Setup the local environment<\/h2>\n<p dir=\"auto\">Make sure you have\u00a0<a href=\"https:\/\/www.python.org\/\" rel=\"nofollow\">Python<\/a>\u00a0=&gt; 3.8.10 installed on your machine.<\/p>\n<p dir=\"auto\">To confirm you can run\u00a0<code>python<\/code>\u00a0or\u00a0<code>python3<\/code>\u00a0on terminal.<\/p>\n<div class=\"highlight highlight-source-python position-relative overflow-auto\">\n<pre><span class=\"pl-s1\">python<\/span> <span class=\"pl-c1\">-<\/span><span class=\"pl-c1\">-<\/span><span class=\"pl-s1\">version<\/span><\/pre>\n<\/div>\n<p dir=\"auto\"><div class=\"alert alert-primary\"><p class=\"alert-divider\"><i class=\"fabric-icon fabric-icon--Info\"><\/i><strong>Note<\/strong><\/p>All the commands shown here are for Windows. If you are working on any other OS\/ environment e.g., Linux, MAC etc. change these commands accordingly.<\/div><\/p>\n<p dir=\"auto\">Make sure you have\u00a0<a href=\"https:\/\/packaging.python.org\/en\/latest\/guides\/installing-using-pip-and-virtual-environments\/\" rel=\"nofollow\">venv<\/a>\u00a0installed and create a new virtual environment in the folder where you have cloned the repository:<\/p>\n<div class=\"highlight highlight-source-shell position-relative overflow-auto\">\n<pre>python3 -m venv env<\/pre>\n<\/div>\n<p dir=\"auto\"><div class=\"alert alert-primary\"><p class=\"alert-divider\"><i class=\"fabric-icon fabric-icon--Info\"><\/i><strong>Note<\/strong><\/p>In the above command the second parameter <code>env<\/code> is the location to create virtual environment. <code>venv<\/code> will create a virtual Python installation in the <code>env<\/code> folder. You should exclude your virtual environment directory from your version control system using <code>.gitignore<\/code> or similar.<\/div><\/p>\n<p dir=\"auto\">Before you start installing or using Django packages in your virtual environment, you&#8217;ll need to <a href=\"https:\/\/packaging.python.org\/en\/latest\/guides\/installing-using-pip-and-virtual-environments\/#activating-a-virtual-environment\" rel=\"nofollow\">activate it<\/a>, for example on Windows:<\/p>\n<pre><code>.<span class=\"pl-cce\">\\e<\/span>nv<span class=\"pl-cce\">\\S<\/span>cripts<span class=\"pl-cce\">\\a<\/span>ctivate<\/code><\/pre>\n<\/div>\n<div>\n<p><div class=\"alert alert-primary\"><p class=\"alert-divider\"><i class=\"fabric-icon fabric-icon--Info\"><\/i><strong>Note<\/strong><\/p>You can confirm, you\u2019re in the virtual environment by checking the location of your Python interpreter. It should be in the <code>env<\/code> directory. Run the command <code>'where python'<\/code> to double check that.\u00a0As long as your virtual environment is activated, <code>pip<\/code> will install packages into that specific environment, and you\u2019ll be able to import and use packages in your Python application.<\/div><\/p>\n<h2>Install the dependencies<\/h2>\n<p dir=\"auto\">Make sure virtual environment is active and you are into your\u00a0<code>&lt;working_folder&gt;\\azure-sql-db-django<\/code>.<\/p>\n<p dir=\"auto\"><div class=\"alert alert-success\"><p class=\"alert-divider\"><i class=\"fabric-icon fabric-icon--Lightbulb\"><\/i><strong>Tip<\/strong><\/p>You can install all the required packages in one go by running the below command and directly move to Database ConnectionString <strong>Configuration<\/strong>\u00a0section or may follow the instructions to execute them one by one:<\/p>\n<p dir=\"auto\"><code>pip install -r .\\requirements.txt<\/code><\/div><\/p>\n<p dir=\"auto\">Install\u00a0<a href=\"https:\/\/www.djangoproject.com\/download\/\" rel=\"nofollow\">Django<\/a>:<\/p>\n<div class=\"highlight highlight-source-python position-relative overflow-auto\">\n<pre><span class=\"pl-s1\">pip<\/span> <span class=\"pl-s1\">install<\/span> <span class=\"pl-s1\">django<\/span><\/pre>\n<\/div>\n<p dir=\"auto\">Also, install Django REST framework for REST API:<\/p>\n<div class=\"highlight highlight-source-python position-relative overflow-auto\">\n<pre><span class=\"pl-s1\">pip<\/span> <span class=\"pl-s1\">install<\/span> <span class=\"pl-s1\">djangorestframework<\/span><\/pre>\n<\/div>\n<p dir=\"auto\">You should also install\u00a0<a href=\"https:\/\/pypi.org\/project\/django-cors-headers\/\" rel=\"nofollow\">django-cors-headers<\/a>. It&#8217;s a Django application for handling the server headers required for Cross-Origin Resource Sharing (CORS).<\/p>\n<div class=\"highlight highlight-source-python position-relative overflow-auto\">\n<pre><span class=\"pl-s1\">pip<\/span> <span class=\"pl-s1\">install<\/span> <span class=\"pl-s1\">django<\/span><span class=\"pl-c1\">-<\/span><span class=\"pl-s1\">cors<\/span><span class=\"pl-c1\">-<\/span><span class=\"pl-s1\">headers<\/span><\/pre>\n<\/div>\n<p><div class=\"alert alert-warning\"><p class=\"alert-divider\"><i class=\"fabric-icon fabric-icon--Warning\"><\/i><strong>Warning<\/strong><\/p>Adding <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTTP\/CORS\">CORS<\/a> headers allows your resources to be accessed on other domains. It\u2019s important you understand the implications before adding the headers since you could be unintentionally opening up your site\u2019s private data to others.<\/div><\/p>\n<h2>Configure Azure SQL connectivity with Django App<\/h2>\n<h3>Dependencies<\/h3>\n<ul>\n<li>pyodbc 3.0 or newer<\/li>\n<li>Microsoft SQL Server ODBC driver<\/li>\n<\/ul>\n<h3>Installation<\/h3>\n<ul>\n<li>\n<p dir=\"auto\">Install ODBC driver:\u00a0<a href=\"https:\/\/docs.microsoft.com\/en-us\/sql\/connect\/odbc\/microsoft-odbc-driver-for-sql-server?view=sql-server-ver15\" rel=\"nofollow\">Instructions<\/a><\/p>\n<\/li>\n<li>\n<p dir=\"auto\">Install pyodbc:<\/p>\n<div class=\"highlight highlight-source-python position-relative overflow-auto\">\n<pre><span class=\"pl-s1\">pip<\/span> <span class=\"pl-s1\">install<\/span> <span class=\"pl-s1\">pyodbc<\/span><\/pre>\n<\/div>\n<\/li>\n<li>\n<p dir=\"auto\">Install mssql-django:<\/p>\n<div class=\"highlight highlight-source-python position-relative overflow-auto\">\n<pre><span class=\"pl-s1\">pip<\/span> <span class=\"pl-s1\">install<\/span> <span class=\"pl-s1\">mssql<\/span><span class=\"pl-c1\">-<\/span><span class=\"pl-s1\">django<\/span><\/pre>\n<\/div>\n<\/li>\n<\/ul>\n<h3>Configuration<\/h3>\n<p dir=\"auto\">Configure the Database ConnectionString in the\u00a0<code>settings.py<\/code>\u00a0file used by your Django project to use\u00a0<code>mssql<\/code>\u00a0and the related ODBC driver.<\/p>\n<div class=\"highlight highlight-source-sql position-relative overflow-auto\">\n<pre>DATABASES <span class=\"pl-k\">=<\/span> {\r\n    <span class=\"pl-s\"><span class=\"pl-pds\">'<\/span>default<span class=\"pl-pds\">'<\/span><\/span>: {\r\n        <span class=\"pl-s\"><span class=\"pl-pds\">'<\/span>ENGINE<span class=\"pl-pds\">'<\/span><\/span>: <span class=\"pl-s\"><span class=\"pl-pds\">'<\/span>mssql<span class=\"pl-pds\">'<\/span><\/span>,\r\n        <span class=\"pl-s\"><span class=\"pl-pds\">'<\/span>PORT<span class=\"pl-pds\">'<\/span><\/span>: <span class=\"pl-s\"><span class=\"pl-pds\">'<\/span>1433<span class=\"pl-pds\">'<\/span><\/span>,\r\n        <span class=\"pl-s\"><span class=\"pl-pds\">'<\/span>OPTIONS<span class=\"pl-pds\">'<\/span><\/span>: {\r\n                <span class=\"pl-s\"><span class=\"pl-pds\">'<\/span>driver<span class=\"pl-pds\">'<\/span><\/span>: <span class=\"pl-s\"><span class=\"pl-pds\">'<\/span>ODBC Driver 17 for SQL Server<span class=\"pl-pds\">'<\/span><\/span>,\r\n            },\r\n    }\r\n}<\/pre>\n<\/div>\n<p dir=\"auto\">To connect Azure SQL DB using MSI (Managed Service Identity), you can have settings as below:<\/p>\n<div class=\"highlight highlight-source-sql position-relative overflow-auto\">\n<pre>DATABASES <span class=\"pl-k\">=<\/span> {\r\n    <span class=\"pl-s\"><span class=\"pl-pds\">'<\/span>default<span class=\"pl-pds\">'<\/span><\/span>: {\r\n         <span class=\"pl-s\"><span class=\"pl-pds\">'<\/span>ENGINE<span class=\"pl-pds\">'<\/span><\/span>: <span class=\"pl-s\"><span class=\"pl-pds\">'<\/span>mssql<span class=\"pl-pds\">'<\/span><\/span>,\r\n         <span class=\"pl-s\"><span class=\"pl-pds\">'<\/span>Trusted_Connection<span class=\"pl-pds\">'<\/span><\/span>: <span class=\"pl-s\"><span class=\"pl-pds\">'<\/span>no<span class=\"pl-pds\">'<\/span><\/span>, \r\n         <span class=\"pl-s\"><span class=\"pl-pds\">'<\/span>OPTIONS<span class=\"pl-pds\">'<\/span><\/span>: { \r\n             <span class=\"pl-s\"><span class=\"pl-pds\">'<\/span>driver<span class=\"pl-pds\">'<\/span><\/span>: <span class=\"pl-s\"><span class=\"pl-pds\">'<\/span>ODBC Driver 17 for SQL Server<span class=\"pl-pds\">'<\/span><\/span>, \r\n             <span class=\"pl-s\"><span class=\"pl-pds\">'<\/span>extra_params<span class=\"pl-pds\">'<\/span><\/span>: <span class=\"pl-s\"><span class=\"pl-pds\">\"<\/span>Authentication=ActiveDirectoryMsi;Encrypt=yes;TrustServerCertificate=no<span class=\"pl-pds\">\"<\/span><\/span> }\r\n     }\r\n}<\/pre>\n<\/div>\n<p dir=\"auto\"><code><\/code><\/p>\n<p dir=\"auto\">Please note that for this sample we decided to avoid having secrets in the <code>settings.py<\/code> file. All sensitive details will be loaded from environment variables. For development purposes you can create an <code>.env<\/code>\u00a0file, using the provided\u00a0<code>.env.sample<\/code>, to provide database connection info.<code><\/code><\/p>\n<p dir=\"auto\"><div class=\"alert alert-warning\"><p class=\"alert-divider\"><i class=\"fabric-icon fabric-icon--Warning\"><\/i><strong>Warning<\/strong><\/p><a href=\"https:\/\/github.com\/microsoft\/mssql-django\">mssql-django<\/a> doesn\u2019t support using time zones yet, so the recommendation is to ensure the <code>USE_TZ<\/code> option is set to <code>False<\/code>.<\/p>\n<p dir=\"auto\"><sub><code>DATABASES = { ... } <\/code><\/sub><\/p>\n<p dir=\"auto\"><sub><code># set this to False if the backend does not support using time zones <\/code><\/sub><\/p>\n<p dir=\"auto\"><sub><code>USE_TZ = False<\/code><\/sub><\/div><\/p>\n<p dir=\"auto\">Run the migrations command to propagate changes you made to your models (creating a class, adding a field, deleting a model, etc.) into your database schema.<\/p>\n<div class=\"highlight highlight-source-python position-relative overflow-auto\">\n<pre><span class=\"pl-s1\">python<\/span> <span class=\"pl-s1\">manage<\/span>.<span class=\"pl-s1\">py<\/span> <span class=\"pl-s1\">makemigrations<\/span> <span class=\"pl-s1\">customerapi<\/span>\r\n\r\n<span class=\"pl-s1\">python<\/span> <span class=\"pl-s1\">manage<\/span>.<span class=\"pl-s1\">py<\/span> <span class=\"pl-s1\">migrate<\/span> <span class=\"pl-s1\">customerapi<\/span><\/pre>\n<\/div>\n<p dir=\"auto\">Once migration is done successfully, you&#8217;ll see that database objects are created in your database. You can connect to your database and verify. Quickstart available here:\u00a0<a href=\"https:\/\/docs.microsoft.com\/en-us\/sql\/azure-data-studio\/quickstart-sql-database?view=sql-server-ver15\" rel=\"nofollow\">Quickstart: Use Azure Data Studio to connect and query Azure SQL database<\/a><\/p>\n<h2>Run sample locally<\/h2>\n<p dir=\"auto\">Execute the below command, to start the development web server on the local machine. By default, the server runs on port 8000 on the IP address 127.0.0.1. You can pass in an IP address and port number explicitly.<\/p>\n<p dir=\"auto\">Initialize Django &#8211; this is needed only the first time<\/p>\n<div class=\"highlight highlight-source-python position-relative overflow-auto\">\n<pre><span class=\"pl-s1\">python<\/span> <span class=\"pl-s1\">manage<\/span>.<span class=\"pl-s1\">py<\/span> <span class=\"pl-s1\">migrate<\/span>\r\n\r\n<span class=\"pl-s1\">python<\/span> <span class=\"pl-s1\">manage<\/span>.<span class=\"pl-s1\">py<\/span> <span class=\"pl-s1\">createsuperuser<\/span><\/pre>\n<\/div>\n<p dir=\"auto\">and then run the server<\/p>\n<div class=\"highlight highlight-source-python position-relative overflow-auto\">\n<pre><span class=\"pl-s1\">python<\/span> <span class=\"pl-s1\">manage<\/span>.<span class=\"pl-s1\">py<\/span> <span class=\"pl-s1\">runserver<\/span><\/pre>\n<\/div>\n<p dir=\"auto\">Once the Django application is running, you&#8217;ll see something like:<\/p>\n<div class=\"highlight highlight-source-shell position-relative overflow-auto\">\n<pre>...\r\nSystem check identified no issues (0 silenced).\r\nFebruary 04, 2022 - 14:32:15\r\nDjango version 4.0.2, using settings <span class=\"pl-s\"><span class=\"pl-pds\">'<\/span>django-sql-project.settings<span class=\"pl-pds\">'<\/span><\/span>\r\nStarting development server at http:\/\/127.0.0.1:8000\/\r\nQuit the server with CTRL-BREAK.<\/pre>\n<\/div>\n<p dir=\"auto\">Using a REST Client (like\u00a0<a href=\"https:\/\/insomnia.rest\/\" rel=\"nofollow\">Insomnia<\/a>,\u00a0<a href=\"https:\/\/www.postman.com\/\" rel=\"nofollow\">Postman<\/a>, or curl), you can now call your API, for example:<\/p>\n<div class=\"highlight highlight-source-shell position-relative overflow-auto\">\n<pre>curl -X GET http:\/\/127.0.0.1:8000\/customerapi\/customer\/<\/pre>\n<\/div>\n<p dir=\"auto\">And you\u2019ll get a response something like (based on available data in tables):<\/p>\n<div class=\"highlight highlight-source-json position-relative overflow-auto\">\n<pre>[\r\n    {<span class=\"pl-ent\">\"CustomerId\"<\/span>: <span class=\"pl-c1\">1<\/span>, <span class=\"pl-ent\">\"CustomerName\"<\/span>: <span class=\"pl-s\"><span class=\"pl-pds\">\"<\/span>Keith<span class=\"pl-pds\">\"<\/span><\/span>},{<span class=\"pl-ent\">\"CustomerId\"<\/span>: <span class=\"pl-c1\">2<\/span>, <span class=\"pl-ent\">\"CustomerName\"<\/span>: <span class=\"pl-s\"><span class=\"pl-pds\">\"<\/span>Janet<span class=\"pl-pds\">\"<\/span><\/span>},{<span class=\"pl-ent\">\"CustomerId\"<\/span>: <span class=\"pl-c1\">4<\/span>, <span class=\"pl-ent\">\"CustomerName\"<\/span>: <span class=\"pl-s\"><span class=\"pl-pds\">\"<\/span>Cortana<span class=\"pl-pds\">\"<\/span><\/span>},{<span class=\"pl-ent\">\"CustomerId\"<\/span>: <span class=\"pl-c1\">5<\/span>, <span class=\"pl-ent\">\"CustomerName\"<\/span>: <span class=\"pl-s\"><span class=\"pl-pds\">\"<\/span>Michael<span class=\"pl-pds\">\"<\/span><\/span>},{<span class=\"pl-ent\">\"CustomerId\"<\/span>: <span class=\"pl-c1\">7<\/span>, <span class=\"pl-ent\">\"CustomerName\"<\/span>: <span class=\"pl-s\"><span class=\"pl-pds\">\"<\/span>David<span class=\"pl-pds\">\"<\/span><\/span>},{<span class=\"pl-ent\">\"CustomerId\"<\/span>: <span class=\"pl-c1\">8<\/span>, <span class=\"pl-ent\">\"CustomerName\"<\/span>: <span class=\"pl-s\"><span class=\"pl-pds\">\"<\/span>Mike<span class=\"pl-pds\">\"<\/span><\/span>}\r\n]<\/pre>\n<\/div>\n<p dir=\"auto\">Check out the\u00a0<a href=\"https:\/\/github.com\/azure-samples\/azure-sql-db-django\">sample<\/a>\u00a0to test all the CRUD operations.<\/p>\n<h2>Deploy your application code to Azure App Service<\/h2>\n<p dir=\"auto\">Azure App service supports multiple methods to deploy your application code to Azure including support for GitHub Actions and all major CI\/CD tools. This article focuses on how to deploy your code from your local workstation to Azure.<\/p>\n<h3>Prerequisites<\/h3>\n<p dir=\"auto\">If you don&#8217;t have an Azure subscription, create a\u00a0<a href=\"https:\/\/azure.microsoft.com\/en-us\/free\/\" rel=\"nofollow\">free<\/a>\u00a0account before you begin.<\/p>\n<p dir=\"auto\">This article requires that you&#8217;re running the Azure CLI version 2.0 or later locally. To see the version installed, run the\u00a0<code>az --version<\/code>\u00a0command. If you need to install or upgrade, see\u00a0<a href=\"https:\/\/docs.microsoft.com\/en-us\/cli\/azure\/install-azure-cli\" rel=\"nofollow\">Install Azure CLI<\/a>.<\/p>\n<p dir=\"auto\">You&#8217;ll need to login to your account using the\u00a0<a href=\"https:\/\/docs.microsoft.com\/en-us\/cli\/azure\/authenticate-azure-cli\" rel=\"nofollow\">az login<\/a>\u00a0command.<\/p>\n<div class=\"snippet-clipboard-content position-relative overflow-auto\">\n<pre><code>az login\r\n<\/code><\/pre>\n<\/div>\n<p dir=\"auto\">If you have multiple subscriptions, choose the appropriate subscription in which the resource should be created. Select the specific subscription ID under your account using\u00a0<a href=\"https:\/\/docs.microsoft.com\/en-us\/cli\/azure\/account?view=azure-cli-latest\" rel=\"nofollow\">az account set<\/a>\u00a0command. Substitute the subscription ID property from the az login output for your subscription into the subscription ID placeholder.<\/p>\n<div class=\"snippet-clipboard-content position-relative overflow-auto\">\n<pre><code>az account set --subscription &lt;subscription id&gt;\r\n<\/code><\/pre>\n<\/div>\n<blockquote>\n<p dir=\"auto\">Refer\u00a0<a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/app-service\/configure-language-python\" rel=\"nofollow\">this article<\/a>, to know more about configuring a Linux Python app for Azure app Service.<\/p>\n<\/blockquote>\n<h3>Configure static files<\/h3>\n<ul>\n<li>In your settings file, define\u00a0<code>STATIC_URL<\/code>\u00a0and\u00a0<code>STATIC_ROOT<\/code>, for example:<\/li>\n<\/ul>\n<div class=\"highlight highlight-source-python position-relative overflow-auto\">\n<pre><span class=\"pl-c\"># Static files (CSS, JavaScript, Images)<\/span>\r\n<span class=\"pl-c\"># https:\/\/docs.djangoproject.com\/en\/4.0\/howto\/static-files\/<\/span>\r\n\r\n<span class=\"pl-v\">STATIC_URL<\/span> <span class=\"pl-c1\">=<\/span> <span class=\"pl-s\">'\/static\/'<\/span>\r\n<span class=\"pl-v\">STATIC_ROOT<\/span> <span class=\"pl-c1\">=<\/span> <span class=\"pl-s\">'static'<\/span><\/pre>\n<\/div>\n<ul>\n<li>Run the\u00a0<code>python manage.py collectstatic<\/code>\u00a0to gather static files into a directory at\u00a0<code>STATIC_ROOT<\/code>\u00a0path for the admin site:<\/li>\n<\/ul>\n<div class=\"highlight highlight-source-python position-relative overflow-auto\">\n<pre><span class=\"pl-s1\">python<\/span> <span class=\"pl-s1\">manage<\/span>.<span class=\"pl-s1\">py<\/span> <span class=\"pl-s1\">collectstatic<\/span><\/pre>\n<\/div>\n<h3>Create the App Service webapp and deploy code from a local workspace<\/h3>\n<p dir=\"auto\">In the terminal, make sure you&#8217;re in the repository root (<code>&lt;working_folder&gt;\\azure-sql-db-django<\/code>) that contains the app code.<\/p>\n<p dir=\"auto\">Run the below\u00a0<a href=\"https:\/\/docs.microsoft.com\/en-us\/cli\/azure\/webapp?view=azure-cli-latest\" rel=\"nofollow\">az webapp<\/a>\u00a0commands:<\/p>\n<blockquote>\n<p dir=\"auto\"><a href=\"https:\/\/docs.microsoft.com\/en-us\/cli\/azure\/webapp?view=azure-cli-latest#az-webapp-up\" rel=\"nofollow\">az webapp up<\/a>\u00a0create a webapp and deploy code from a local workspace to the app. Python apps are created as <strong>Linux<\/strong> apps by default.<\/p>\n<\/blockquote>\n<div class=\"snippet-clipboard-content position-relative overflow-auto\">\n<pre><code># Create a web app and deploy the code\r\naz webapp up -g &lt;MyResourceGroup&gt; -l &lt;location&gt; -p &lt;azure-sql-db-django-plan&gt; --sku B1 -n &lt;azure-sql-db-django-api&gt; -r 'PYTHON:3.9'\r\n\r\n# Configure database information as environment variables\r\naz webapp config appsettings set --settings DB_SERVER=\"&lt;azure-sql-server-name&gt;.database.windows.net\" DB_NAME=\"&lt;db-name&gt;\" DB_USER=\"&lt;db-user-id&gt;\" DB_PASSWORD=\"&lt;db-password&gt;\"\r\n<\/code><\/pre>\n<\/div>\n<ul>\n<li>For the\u00a0<code>--resource-group -g<\/code>, you can use the same resource group you created for the Database in the previous section.<\/li>\n<li>For the\u00a0<code>--location -l<\/code>\u00a0argument, use the same location as you did for the database in the previous section.<\/li>\n<li>Create the\u00a0<a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/app-service\/overview-hosting-plans\" rel=\"nofollow\">App Service plan<\/a>\u00a0<em>azure-sql-db-django-plan<\/em>\u00a0in the Basic pricing tier (B1), if it doesn&#8217;t exist. &#8211;plan and &#8211;sku are optional.<\/li>\n<li>For the\u00a0<code>--runtime -r<\/code>, canonicalize runtime in the format of Framework|Version, e.g. &#8220;PYTHON|3.9&#8221;. Allowed delimiters: &#8220;|&#8221; or &#8220;:&#8221;. Use\u00a0<code>az webapp list-runtimes --linux --output table<\/code>\u00a0for available list.<\/li>\n<li>The app code expects to find database information in a number of environment variables. To set environment variables in App Service, you create &#8220;app settings&#8221; with the\u00a0<a href=\"https:\/\/docs.microsoft.com\/en-us\/cli\/azure\/webapp\/config\/appsettings?view=azure-cli-latest#az_webapp_config_appsettings_set\" rel=\"nofollow\">az webapp config appsettings set<\/a>\u00a0command.<\/li>\n<\/ul>\n<p><div class=\"alert alert-success\"><p class=\"alert-divider\"><i class=\"fabric-icon fabric-icon--Lightbulb\"><\/i><strong>Tip<\/strong><\/p>App Service detects a Django project by looking for a wsgi.py file in each subfolder, which <code>manage.py startproject<\/code> creates by default. When App Service finds that file, it loads the Django web app. For more information, see <a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/app-service\/configure-language-python\">Configure built-in Python image<\/a>.<\/div><\/p>\n<h2>Browse to the app running on Azure App Service<\/h2>\n<p dir=\"auto\">The Python Django sample code is running a Linux container in App Service using a built-in image.<\/p>\n<p dir=\"auto\">Browse to the deployed application in your web browser at the URL\u00a0<code>https:\/\/&lt;app-name&gt;.azurewebsites.net\/admin<\/code>\u00a0or make a call to the API\u00a0<code>https:\/\/&lt;app-name&gt;.azurewebsites.net\/customerapi\/customer\/<\/code>\u00a0using any other REST clients (like\u00a0<a href=\"https:\/\/insomnia.rest\/\" rel=\"nofollow\">Insomnia<\/a>,\u00a0<a href=\"https:\/\/www.postman.com\/\" rel=\"nofollow\">Postman<\/a>, or curl).<\/p>\n<p dir=\"auto\"><strong>Congratulations!<\/strong> You&#8217;re running a Python Django app in Azure App Service for Linux, with Azure SQL database.<\/p>\n<p dir=\"auto\"><div class=\"alert alert-success\"><p class=\"alert-divider\"><i class=\"fabric-icon fabric-icon--Lightbulb\"><\/i><strong>Tip<\/strong><\/p>You can use <a href=\"https:\/\/github.com\/microsoft\/mssql-django\">mssql-django<\/a> as a backend for your existing Django 4.0 project with no major change if that\u2019s already configured for MS SQL Server.<\/div><\/p>\n<p dir=\"auto\">If you encounter any issues or have any feedback about\u00a0<a href=\"https:\/\/github.com\/microsoft\/mssql-django\">mssql-django<\/a>, head over to our mssql-django project repository and submit an issue.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Django is a Python-based open-source web framework. It is a popular and well-liked web framework among developers all around the world. But wouldn&#8217;t it be amazing to build a website with Django while also taking advantage of Azure SQL database&#8217;s security, performance, high availability, and other great capabilities? In this article, we will create a [&hellip;]<\/p>\n","protected":false},"author":29335,"featured_media":81,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1,411],"tags":[465,507,407],"class_list":["post-1176","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-azure-sql","category-python","tag-azuresql","tag-django","tag-python"],"acf":[],"blog_post_summary":"<p>Django is a Python-based open-source web framework. It is a popular and well-liked web framework among developers all around the world. But wouldn&#8217;t it be amazing to build a website with Django while also taking advantage of Azure SQL database&#8217;s security, performance, high availability, and other great capabilities? In this article, we will create a [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/posts\/1176","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/users\/29335"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/comments?post=1176"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/posts\/1176\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/media\/81"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/media?parent=1176"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/categories?post=1176"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/tags?post=1176"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}