AppService/_posts/2017-5-10-Create Django Web...

136 строки
8.0 KiB
HTML
Исходник Постоянная ссылка Ответственный История

Этот файл содержит невидимые символы Юникода!

Этот файл содержит невидимые символы Юникода, которые могут быть отображены не так, как показано ниже. Если это намеренно, можете спокойно проигнорировать это предупреждение. Используйте кнопку Экранировать, чтобы показать скрытые символы.

---
title: "Create Django Web app with PostgreSQL"
hide_excerpt: true
---
<html><head>
<meta charset="utf-8"/>
</head>
<body>
<div id="page">
<a class="url fn n profile-usercard-hover" href="https://social.msdn.microsoft.com/profile/mksunitha" target="_blank">mksunitha</a>
<time> 5/10/2017 8:00:57 AM</time>
<hr/>
<div id="content">This tutorial describes how to get started running Django on <a href="https://azure.microsoft.com/en-us/services/app-service-web">Azure Web Apps</a> with <a href="https://azure.microsoft.com/en-us/services/postgresql/">Azure Database for PostgreSQL(Preview)</a>. Web Apps provides limited free hosting and rapid deployment, and you can use Python.
<h2>Before you begin</h2>
<ul></ul>
If you don't have an Azure subscription, create a <a href="https://azure.microsoft.com/free/?WT.mc_id=A261C142F">free account</a> before you begin.
<h2>Deploy from Marketplace</h2>
This blog post describes how to get started running a Django app with postgresql from Azure marketplace . This marketplace solution creates the following resources : <b>Web Apps on Windows </b> and <b>Azure Database for PostgreSQL (Preview)</b>.
Log in to the <a href="https://portal.azure.com">Azure Portal</a>. Launch the <a href="https://portal.azure.com/#create/Django.djangopostgresql">Django + PostgreSQL template</a> in the Azure marketplace to get started. Provide the necessary information for web app and database to be deployed.
<img alt="djangopg1" class="alignnone size-large wp-image-4445" height="891" src="{{ site.baseurl }}/media/2017/05/djangopg1-1010x1024.png" width="879"/>
 
<table>
<thead>
<tr>
<th>Name</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>App Name</td>
<td>
<div>
<div><span>Enter a unique app name for your </span><span>**Web App Name**</span><span>. This name is used as part of the default DNS name for your app </span><em>&lt;app_name&gt;.azurewebsites.net</em><span>, 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 </span></div>
</div></td>
</tr>
<tr>
<td>Subscription</td>
<td>Select a <span><strong>Subscription</strong></span><span>. If you have multiple subscriptions, choose the appropriate subscription.</span></td>
</tr>
<tr>
<td>Resource group</td>
<td>
<div>
<div><span>Enter a </span><span><strong>resource group</strong></span><span>. 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</span></div>
</div></td>
</tr>
<tr>
<td>App Service Plan</td>
<td>
<div>
<div style="text-align: left"><span>App Service plans represent the collection of physical resources used to host your apps. Select the </span><span><strong>Location</strong></span><span> and the </span><span><strong>Pricing tier</strong></span><span>. For more information on pricing, see <a href="https://azure.microsoft.com/pricing/details/app-service"> App service pricing tier</a> </span></div>
</div></td>
</tr>
<tr>
<td>Server Name</td>
<td>Enter a postgresql database servername</td>
</tr>
<tr>
<td>Server admin login name</td>
<td><span>Enter a postgresql database administrator username </span></td>
</tr>
<tr>
<td>Server admin password</td>
<td><span>Enter a postgresql database administrator password</span></td>
</tr>
<tr>
<td>Version</td>
<td><span>Azure database for PostgreSQL(Preview) currenlty supports PostgreSQL 9.5 version</span></td>
</tr>
<tr>
<td>Pricing tier</td>
<td><span>Choose Basic or Standard pricing tier. For more information on pricing, see <a href="https://azure.microsoft.com/pricing/details/postgres/"> App service pricing tier</a> </span></td>
</tr>
<tr>
<td>Database Name</td>
<td><span>Enter a database name for your web app</span></td>
</tr>
</tbody>
</table>
You can watch the progress by clicking the bell icon at the top of the portal page while the app is being deployed.
<a href="{{ site.baseurl }}/media/2017/05/deploy-success.png"><img alt="deploy-success" class="alignnone wp-image-4455" height="100" src="{{ site.baseurl }}/media/2017/05/deploy-success.png" width="413"/></a>
When the web app creation is finished, navigate in the Azure portal to the resource group to view the web app and PostgreSQL server.
<a href="{{ site.baseurl }}/media/2017/05/resources-djngo.png"><img alt="resources-djngo" class="alignnone size-large wp-image-4465" height="297" src="{{ site.baseurl }}/media/2017/05/resources-djngo-1024x346.png" width="879"/></a>
Select the web app line and then click <strong>Browse</strong>.
<a href="{{ site.baseurl }}/media/2017/05/django-browse.png"><img alt="django-browse" class="alignnone size-large wp-image-4475" height="217" src="{{ site.baseurl }}/media/2017/05/django-browse-1024x253.png" width="879"/></a>
<h2>Develop your application</h2>
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 <strong>Advanced Tools -&gt; Go </strong>. <a href="{{ site.baseurl }}/media/2017/05/django-kudu.png"><img alt="django-kudu" class="alignnone size-large wp-image-4485" height="192" src="{{ site.baseurl }}/media/2017/05/django-kudu-1024x224.png" width="879"/></a>
Click on <strong>Debug Console </strong> to access the <strong>CMD </strong> prompt. In the console run the following commands  under wwwroot folder.
<pre>env\scripts\python.exe env\scripts\django-admin.exe startapp myapp</pre>
<a href="{{ site.baseurl }}/media/2017/05/console-django.png"><img alt="console-django" class="alignnone size-large wp-image-4505" height="87" src="{{ site.baseurl }}/media/2017/05/console-django-1024x101.png" width="879"/></a>
This will create <strong>myapp </strong>folder with starter Django app . For more details in Django app, see <a href="https://docs.djangoproject.com/en/1.11/intro/tutorial01/">Get started with Django</a> .
<a href="{{ site.baseurl }}/media/2017/05/create-app-django.png"><img alt="create-app-django" class="alignnone wp-image-4506" height="249" src="{{ site.baseurl }}/media/2017/05/create-app-django-1024x436.png" width="585"/></a>
<h2>Database configuration</h2>
You can access the database information within Azure portal by clicking in <b>Application settings -&gt; Connection strings</b>. It is best practice to use App Settings for storing your database information instead of hard coding it in your <em>settings.py</em> file. Create these app settings in Azure portal for your web app.
<img alt="django-appsettings" class="alignnone size-large wp-image-4516" height="211" src="{{ site.baseurl }}/media/2017/05/django-appsettings-1024x246.png" width="879"/>
Update DATABASES setting in local <em>settings.py</em> file in your application. to read from <b>App Settings</b> environment variables.
<pre>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',
}
}
</pre>
<h2>Database Management</h2>
Use <a href="https://www.pgadmin.org/">PgAdmin</a> PostgreSQL Client to manage your PostgreSQL server and database remotely.
<h2>Django Poll application sample</h2>
You can deploy your own app via GIT or use this sample <a href="https://github.com/SunBuild/django-poll">Django-poll application</a>. Fork this sample repository or download to locally to start using the sample. For more information to setup GIT, see <a href="https://docs.microsoft.com/en-us/azure/app-service-web/app-service-deploy-local-git">Local Git Deployment to Azure App Service</a>.
<h2>Next Steps</h2>
<ul>
<li><a href="https://www.djangoproject.com/">Django Documentation</a></li>
<li><a href="http://aka.ms/ptvsdocs">Python Tools for Visual Studio Documentation</a></li>
</ul></div>
</div></body>
<script src="{{ site.baseurl }}/resource/jquery-1.12.1.min.js" type="text/javascript"></script>
<script src="{{ site.baseurl }}/resource/replace.js" type="text/javascript"></script>
</html>