1

I've been trying to create a Django web app using VS2017 Preview (which includes Python tools for Visual Studio), and deploy the resulting app to Azure (I'm currently on the 30-day trial period to evaluate Azure).

I've done the following:

  • Start VS2017, create a new project with the "Django Web Project" template. This creates a Django webpage with the bootstrap template - simple, and everything works well locally.

  • In VS, go to Connected Services => Publish, select "Microsft Azure App Service", create a new App Service an an App Plan. The instances are created successfully.

  • Click "Publish" to publish via VS WebDeploy. Everything looks good in the console and it says Publish: 1 succeeded, 0 failed, 0 skipped at the end.

This results in the standard Azure Welcome-start-page hostingstart.html showing, not the Django page. Once I remove that html file, there's only a The page cannot be displayed because an internal server error has occurred.. I've tried various things: Going to portal.azure.com "Application Settings", setting Python version from "Off" to "3.4" (I'd like 3.5 in fact, which one of MS's tutorial uses - but any will do for now) - then there's just a hostingstart-python.html showing, still no Django. I've tried adding a default web.config via "Add => New Item => Azure web.config (FastCGI)" in VS. I've tried editing that web.config with various values of WSGI_HANDLER (e.g. django.core.handlers.wsgi.WSGIHandler()) and DJANGO_SETTINGS_MODULE (e.g. mydjangopage.settings), I tried adding "wfastcgi" to requirements.txt, etc. Always just getting a server error.

I have been trying to do this for several hours now and I've read every possible Deployment help page from Microsoft, their blogs, and the web. All the information seems outdated, having missing pieces of information, or is just not working. At this point I'm quite disappointed and ready to give up. Isn't it supposed to be so simple? Create a new project in VS, hit "Publish", and it's supposed to work? (It's definitely not, I restarted from scratch multiple times and tried so many things.)

Ela782
  • 5,041
  • 5
  • 53
  • 66
  • http://stackoverflow.com/questions/32792236/deploy-django-app-on-azure-only-displays-default-app-even-after-deployment?rq=1 is related but talks only about deploying via git. I'm trying to deploy from within VS with Visual Studio's "Web Deploy". – Ela782 Apr 19 '17 at 21:53
  • My suggestion: PyCharm is hundred times better than VS2017 in every single aspect. I've been using VS2017 for 2 months now, and there's nowhere it's better than PyCharm. I've just used `Ryder` and OMG what a slap in the face for the Microsoft Team. I've clean up all my project and now it looks cleaner (thus easier to debug) in 20 minutes. All the suggestions were very good, and that VS2017 did not suggest anything valuable. Not to talk about the shortcut and non-sense UI (I could give at least 10 examples why VS2017 is so bad compared to Jetbrains products). – Olivier Pons Jan 11 '18 at 21:49
  • What I meant is: make a Django project with PyCharm (try the pro version / 30 days trial), you'll fall in love with Python, Django, and debugging. That's not the case (on the contrary!) with VS2017. – Olivier Pons Jan 11 '18 at 21:51
  • @OlivierPons: I did actually, before posting this question. PyCharm is awesome for Python, it was okay for Django as well, but the deployment to Azure is not great (which was the goal of this question). What I wanted was a as-simple-as-possible, integrated experience of developing a site with Django and deploying to Azure. It turns out nothing in this world is simple (;-) ) and both tools are not able to accomplish that. – Ela782 Jan 12 '18 at 10:30
  • Good to know thanks for sharing this – Olivier Pons Jan 12 '18 at 10:47

3 Answers3

0

I had the same problem. I solved it as follows:

Create a Django Web App via the azure portal:

  1. Go to azure and hit the "plus sign" in the left upper corner for adding a resource
  2. Kick on “Web and Mobile”
  3. Click on “Show all”
  4. Type “Django” into the search field
  5. Select the one that is marked in the picture below and create the resource by following the instructions

Deploy on the newly created resource

  1. Go to Visual Studio and start the deployment
  2. Instead of creating a new azure resource out of VS select the one you just created for the deployment.

Wait a moment.

  1. It takes about 3 minutes until you can see you deployment. Before that you will get a default screen that looks a bit different than the one you already know.
  2. After that you should see the correct django page.
Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
Onkelonk
  • 1
  • 1
  • It doesn't fully work (yet). After deploying and waiting, you'll see your deployed django page, but it's still using some old files. E.g. I get an error when accessing views of the Django I added in VS and published: `Using the URLconf defined in DjangoApp.urls`. So it's still using the old .urls file. Probably many more old files are on Azure. – Ela782 Apr 23 '17 at 01:48
  • And if I edit `web.config` on azure, replacing `DJANGO_SETTINGS_MODULE` `DjangoApp.settings` with `mytutorial.settings` (where obviously mytutorial is the Django project I created in VS), then there is a server error again. So on the first glance your answer seems working but if you actually try it, you'll realise it's unfortunately not. – Ela782 Apr 23 '17 at 01:53
  • That is kind of a different problem but I experience the same. Did you find a solution? – Onkelonk Apr 23 '17 at 19:41
  • I just tried to delete the web app on azure and start from scratch. I created a new Django web app as described above in another region and still received the old version of my page. That’s a mess! – Onkelonk Apr 23 '17 at 20:04
  • Why would that be a different problem? No I did not find a solution, and to be frank I stopped looking - I already spent several work days on this so at this point I gave up on Azure, and posted the question here. I would really like to have an answer though and give Azure a shot - it's a shame that it's not working. – Ela782 Apr 23 '17 at 23:19
0

Right now, publishing support in VS 2017 is in a bit of a transition period. In the next couple of updates we want to get it back to a one-click system (and in the process, make it possible to publish Python apps from anywhere, not just within VS), but for now there are a couple of manual steps.

(I'll summarize the steps below, but the canonical documentation will be at https://aka.ms/PythonOnAppService - right now it's a blog post with these steps and some of the backstory)

  1. After creating your new site (either through the portal or through VS - publishing content is okay too), install one of the Python site extensions Installing a site extension through the Azure Portal
    (source: windows.net)

  2. Configure your web.config file to have the correct path to the site extension you installed in the scriptProcessor attribute (something like D:\home\python361x64\python.exe - see the description of each extension for the actual paths - VS 2017 also includes item templates to help set these up, so look through Add New Item for ideas)

  3. Update the WSGI_HANDLER and DJANGO_SETTINGS_MODULE variables as necessary (a typical value for WSGI_HANDLER for a Django app is myapp.wsgi.application, assuming you have a wsgi.py file in your project)

  4. Publish your site through VS.

  5. Use the console to install your packages - e.g. D:\home\python361x64\python.exe -m pip install -r requirements.txt

You may need to restart your site at this point if things were already running, but in general you can now publish quickly through VS without having to reinstall Python or any packages.

If you are deploying your site through ARM with a JSON template, you can also specify the site extension there: (from here)

"resources": [
  {
    "apiVersion": "2015-08-01",
    "name": "[parameters('siteName')]",
    "type": "Microsoft.Web/sites",
    ...
    "resources": [
      {
        "apiVersion": "2015-08-01",
        "name": "python352x64",
        "type": "siteextensions",
        "properties": { },
        "dependsOn": [
          "[resourceId('Microsoft.Web/sites', parameters('siteName'))]"
        ]
      },
    ...
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Zooba
  • 11,221
  • 3
  • 37
  • 40
  • Unfortunately it doesn't work. Can you please clarify on these steps: 1.: If I do it through the portal, add a regular "Web App" or which one? If I want to do it in VS, what do you mean with " or through VS - publishing content is okay too)" - can you please clarify how to do it? PS: Installing the Python extension on the Azure portal failed (supposedly timeout) but still shows up under Extensions now. 2.: I did in VS Add=>New item => Azure web.config (FastCGI). Is that correct, or how to get the web.config? 3.: I don't have a requirements.txt on Azure yet, I think I need to publish first? – Ela782 Apr 26 '17 at 08:56
  • Can you also please clarify on 2.: It has a key `PYTHONPATH` but it wants a folder, not a python.exe. Presumably I need to add the path to the Extension python under `PythonHandler`? Also do I leave `WSGI_HANDLER` as `app.wsgi_app` or put the name of my Django app? I've even seen something like Django.something.GetWSGI() or something like that? Also do I need to add a key for `MyDjangoApp.settings`? – Ela782 Apr 26 '17 at 09:03
  • @Ela782 1. Yes, regular web app. VS has a wizard to create one if you don't want to select an existing one. 2. That's correct. `PYTHONPATH` is the package search path, `scriptProcessor` points to the EXE, `WSGI_HANDLER` should probably be `myapp.wsgi.application` (unless you've changed this) 3. Good point, I'll swap steps 3 and 4. – Zooba May 04 '17 at 16:03
  • By the way, installing the Python Extensions fails half the time with a timeout after a few minutes. Really annoying. Azure is sooo not ready for prime-time and quick development yet. This happened on multiple days, weeks, I even tried different browsers (Edge and Chrome). And it seems sometimes the Extension then still shows as installed and available, even though it says the install failed. Doesn't really inspire confidence in this whole thing. – Ela782 May 11 '17 at 21:23
  • And installing requirements.txt fails: `D:\home\python353x64\python.exe -m pip install -r requirements.txt Bad Request ` I'm still stuck on this - just does not work. @Zooba Can you try this by yourself and provide a more detailed step-by-step guide that actually worked? – Ela782 May 11 '17 at 21:55
  • Okay fixed this one by switching the app service plan to 64-bit in Azure Portal. But still getting `The page cannot be displayed because an internal server error has occurred.`. – Ela782 May 11 '17 at 22:05
  • Okay, I managed to actually get it to run now. I don't think I will ever be able to reproduce this though. Also CSS is not working, no stylesheets at all. Not even running `D:\home\python353x64\python.exe manage.py collectstatic --noinput` fixed it. It works fine locally in VS though... – Ela782 May 11 '17 at 22:13
0
  1. Create a WebApp on Azure (Do not enable python in Application Settings !)

    From deployment option select local git repository: deployment option From properties copy the git url:enter image description here

  2. Copy virtualenv_proxy.py file from https://github.com/Azure-Samples/python-docs-hello-world into your folder.

  3. Create a file runtime.txt and write python-3.4 into it.

  4. Create a file web.3.4.config with content :

<configuration>
  <appSettings>
    <add key="WSGI_ALT_VIRTUALENV_HANDLER" value="django.core.wsgi.get_wsgi_application()" />
    <add key="WSGI_ALT_VIRTUALENV_ACTIVATE_THIS"
         value="D:\home\site\wwwroot\env\Scripts\python.exe" />
    <add key="pythonpath" value="%SystemDrive%\home\site\wwwroot" />
    <add key="WSGI_HANDLER" value="virtualenv_proxy.get_venv_handler()" />
    <add key="WSGI_LOG" value="D:\home\LogFiles\wfastcgi.log"/>
    <add key="DJANGO_SETTINGS_MODULE" value="myModule.settings" />
  </appSettings>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
    <handlers>
      <add name="Python FastCGI" path="handler.fcgi" verb="*" modules="FastCgiModule" scriptProcessor="D:\Python34\python.exe|D:\Python34\Scripts\wfastcgi.py" resourceType="Unspecified" requireAccess="Script" />
    </handlers>
    <rewrite>
      <rules>
        <rule name="Static Files" stopProcessing="true">
          <conditions>
            <add input="true" pattern="false" />
          </conditions>
        </rule>
        <rule name="Configure Python" stopProcessing="true">
          <match url="(.*)" ignoreCase="false" />
          <conditions>
            <add input="{REQUEST_URI}" pattern="^/static/.*" ignoreCase="true" negate="true" />
          </conditions>
          <action type="Rewrite" url="handler.fcgi/{R:1}" appendQueryString="true" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

The only settings you need to change here is the value for DJANGO_SETTINGS_MODULE, replace myModule with your module name. All the over settings and paths should be exactly the same if you created an Web App.

  1. Push everything to the git url you got at point 1, Azure will detect and set up Python 3.4 and install all packages from requirements.txt. After that everything should work. If not, connect using ftp and look into \LogFiles\wfastcgi.log for the error.
Community
  • 1
  • 1
Silencer
  • 1,602
  • 4
  • 17
  • 27