django logo positive

Develop with Django in OS X using Pycharm

Django is a framework for “Perfectionists with deadlines”. This is a throwback to the DRY principle eg Do not repeat yourself. Pretty much most website consist of the same things. Frameworks take all those common tasks and makes is so you don’t have to reinvent the wheel every time you start a new project. We’re going to use Pycharm, which comes in two versions professional edition or the free community version. We’ll be using Homebrew to install python3 and Virtualenv to keep our dependencies separate for different projects.

We’ll set up Pycharm to

  • Automatically activate your Virtualenv session in the terminal and project
  • Set the python environment variable.
  • Run tests and provide coverage
  • Browse the database
  • Run Prospector to lint and check for other code errors.

Setting up OS X dependencies

Install python3

Upgrade pip

Create the .virtualenv directory in your home folder. I had a lot of confusion about where to put .virtualenv  folder initially but this appears to the be the proper place to store your virtualenv’s.

Install virtualenv


Configuring Virtualenv

You can use Pycharm’s Project Interpreter in the project settings to create a virtualenv or select an existing one which we will discuss later or use the following command replacing project-name with your project’s name.

Then to activate the virtualenv we just run

You’ll now see you (project-name) at the beginning of the terminal prompt.


Installing Django and creating a project

We can now install Django

We can start a Django project like so


Django-Edge project skeleton

A more complete place to start is Django-Edge “A Django project skeleton that is modern and cutting edge.”

  • Ready Bootstrap-themed pages
  • User Registration/Sign up
  • Better Security with 12-Factor recommendations
  • Separate settings and pip requirements for production and development environments
  • Logging/Debugging Helpers
  • Works on Python 2.7 or 3.4

We can use the Django-Edge template like so

The directory will look like this.


Installing Django-Edge dependencies

We can install dev dependencies with

This will load all the requirements from the /requirements/base.txt and the /requirements/development.txt .

To load the production dependencies (We still should test production locally) we run

The project settings by default uses the ./src/project-name/settings/development  settings by default which inherits settings from ./src/project-name/settings/base that both production and development using this nifty line from .base import *  at the beginning of each settings file.

The line below for example in the development file adds the django_toolbar app the the installed app array found in the base settings file.


Using Environment Variables in local.env

The Django settings files loads the database information from and other environment variables in the file found at  ./src/project-name/settings/local.env. Django-Edge includes a local.sample.env we can use by copying and renaming it to local.env


Starting the web server

To start the project just run

To tell Django to start the server in production we can use the production settings.

When running other commands such under production settings we can use a temporary environment variable.


Making Migrations

To create the development database and create all the database settings we need to migrate the database. Makemigrations will create a starting point and history of our database. Then migrate will apply those migrations to the database.


Time saving Pycharm settings

Some of these features may only be available in the Professional version which can also be unlocked with a university email account.


Configuring built in Django Support

pycharm-django-settings
Setting Django settings in Pycharm

Creating or setting Virtualenv and installing Pip dependencies

Create a virtual environment in Pycharm note to use the python3 base interpreter we installed using homebrew.

creating virtual enviorment in pycharm
Creating virtual environment in Pycharm

If you inspect  the project files and don’t have dependencies installed you’ll get a nice pop up asking if you’d like to install dependencies.

Install Django dependencies in pycharm
Install Django dependencies in pycharm

Automatically starting Web Server

We can set Pycharm to automatically start the development server when you open the project. The Run/Debug settings are found under the arrow drop down at the top right of the IDE.

Pycharm edit configurations
Pycharm edit configurations

 

Pycharm run Django development server
Pycharm run Django development server

 

Start Django server on Pycharm project opening
Start Django server on Pycharm project opening

Have Pycharm terminal auto activate the Virtualenv and change to project dir

Create the following file called .pycharm-bash

Load custom bash in Pycharm terminal
Load custom bash in Pycharm terminal

Using Pycharms database viewer.

Make sure the database plugin is working.

 

Pycharm plugins enabling django database support
Pycharm plugins enabling Django database support

 

Now you can browse the database.

Pycharm database
Pycharm database

Connecting to a Postgres Database

To connect to a Postgres database open data sources and drivers from the wrench button above the database view. And go ahead an click download to get the postgres drivers.

Pycharm connecting to postgres database
Pycharm connecting to postgres database

The click the arrow at the top left of the data sources and drivers window and add your database settings and apply

connecting pycharm to postgres database
connecting pycharm to postgres database

 

After you set up the correct log in info you should test the connection and get the following

Pycharm postgrest success connection
Pycharm postgrest success connection

 


Pycharm Testing & Coverage

Django-Edge comes with a few tests, I’ve added a pull request that should push coverage up to 96% and run a few extra tests. Allow you to develop much quicker without having to manually check everything works every single time.

pycharm configure testing
pycharm configure testing

As you can see the green bars in the gutter next to the code show which lines are covered giving you key insights into where you should be adding more tests.

pycharm run tests & coverage
Pycharm run tests & coverage

Debugging the front end with the Javascript Debugger.

Make sure to have the Jetbrains IDE Support chrome extention plugin enabled. Then enable/install the Pycharm Live Edit plugin

 

installing pycharm live edit
installing pycharm live edit

 

While your at it change the Live edit update time

pycharm live edit update time
pycharm live edit update time

Then enable it in your run Develop

enable live edit javascript debugger pycharm
enable live edit javascript debugger pycharm

 


Installing Prospector to check for and lint code

 

Then we will open the settings and go to external tools. Set it up as follows. We will use two pycharm macros one that will get the virtualenv interpreter of the current project to find prospector. And the other to set prospector run in the project root folder.

pycharm prospector external tools
pycharm prospector external tools

We should also create the .prospector.yaml for some extra configuration. These are very strict settings requiring docstring, disables the conflicting D203 or add a line before docstring which clashes with no lines before docstring.

 

Leave a Reply