Visual Studio Code with Python Django

I wanted to continue on my python exploration and show how to get started with Visual Studio Code with Python Django.
What is Django you may ask ? Not the movie for sure.

Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel. It’s free and open source.
https://www.djangoproject.com/

Basically you can think of Django somewhat like WebApi plus Entity Framework together, since Django provides an ORM with the framework.

First thing first, in python we should set up our virtualenv such that we have a dev environment and don’t pollute our global space with our packages. You should already have pip install with your python if you are using the Python 3.x series.

Open up your powershell or command prompt and type

Once you have virtualenv we will need virtualenvwrapper-win since we are running under windows.

Now we need to create the virtualenv that we will be using.

This will create your virtualenv inside your home directory for mine it is under

In order to activate the virtualenv we need to use the activate.ps1 script but you must make sure we have the correct execution-policy in your powershell but before we do that lets launch our Vscode

Lets now set up our directory to use the virtualenv, remember the Set-ExecutionPolicy for powershell and use the terminal inside your vscode with Ctrl+Shift+` (right tick)

Lets now install Django

Lets now create a our first Django project.

Note: That we in the root folder manage.py file inside the HelloWorld folder there is another HelloWorld project which contains.

  1. settings.py: Setting is the configuration for this Django project. The file contains all apps, database settings information.
  2. urls.py: The url declarations for this Django project, kind of like the url routes you have in WebApi in global.ascx.cs file, which can list all your urls.
  3. wsgi.py: This is the entry-point for WSGI-compatible web servers to serve the project.
  4. __init__.py: Is an empty file, to signal to Python that this is a Python package.

In order to run our django application we can type

This will start the django server.

VsCode-with-Python-Django-1

VsCode-with-Python-Django-1

Creating our HelloWorld App

Now that we have django up and running we need to create our first app. Hmm have we not create the app yet? Well in Django what we have done so far is we have created a project for Django but we have not created an App yet.
What is the difference between an App and Project then?

What’s the difference between a project and an app? An app is a Web application that does something – e.g., a Weblog system, a database of public records or a simple poll app. A project is a collection of configuration and apps for a particular website. A project can contain multiple apps. An app can be in multiple projects.
https://docs.djangoproject.com/en/1.11/intro/tutorial01/

Lets get started to create an HelloWorldApp

Lets now edit our settings.py under HelloWorld such that we can add the HelloWorldApp into it. Open settings.py in VSCode, and look for INSTALLED_APPS, append our HelloWorldApp to the list (Note the command at the end)

Now let’s modify the urls.py under the HelloWorld project directory.

Now lets modify views.py under HelloWorldApp app directory

We can now run our app

When everything is up and running you should be able to go to Chrome or your favorite browser and view port 8000 and see Hello world there.

VsCode-with-Python-Django-2

VsCode-with-Python-Django-2

Hope this helps you in setting up vscode with development of Python Django.

To learn about running VSCode with python read my previous post on
Getting Started with Visual Studio Code with Python.

Also my post on Flask
Getting Started with Visual Studio Code with Python