Let’s build a basic Django app with the Github API
Today we are going to build a simple app that consumes the JSON response of a simple GitHub API request. We shall be using the following technologies:
Python/Django
Requests
Pipeline
NodeJS
Bower
That is all the technology we shall need. Python and Django will provide the framework for the application. Requests will be used to make a simple request to the Github API. NodeJS and Bower will be used for front end dependency management and Pipeline will do the asset compression.
The Github resources we shall use:
Github API
Primer CSS kit
Octicons
The source code for this tutorial can be found here. By the time we get to the bottom of this, your app should look something like this:
Getting Started
This tutorial assumes a knowledge of virtual environments. The use of virtualenv and virtualenvwrapper is highly recommended. If you don’t have these, you can install them using pip with the following commands:
You should then create a virtual environment. Ours will be called gitit. Get it?
This should activate a virtualenv called Gitit. We shall then install Django, Requests and Pipeline.
When you run the “pip freeze” command it should show the following:
Switch to the directory where you intend to create the project and run the command to start a project.
This should give us the following directory structure
This is the basic directory structure of a Django project with no apps. We shall now proceed to create directories for our static files and the templates.
It now time to run our server just to ensure that everything is fine.
It should run fine, but we need to run migrations to assuage the server gods, so run the migrate command
Let’s create the app
Great, now time to create the app. We shall call it yokogao the Japanese word for profile of a face as seen from the side. This may need clarification :)
Our structure should now have changed to this:
It is now time to customize our settings so as to enable the app work best within the project. Let us add the app to the installed apps
We should now be ready to create the views that allow us to access the Github API and request the basic information we require for our app. For more about the information returned by the API, use Curl
For a simple Curl tutorial that uses the Github API, check this one out. Remember to use your Github username wherever raybesiga is used.
Edit the yokogao/views.py file to look this way:
You should note that we import requests and json. Requests to query the API, and JSON to convert the response returned to a JSON format. You should now create a URLS file at yokogao/urls.py
Head over to gitit/urls.py and edit the file so as to use the Yokogao URLs for the app
If you run your server now, you should be able to see this in your browser, albeit without format
Let us build templates
Great, we are now at the step where we can show stuff in the browser. However, there is business we need to take care of before we can even build our base template. Time to do some front end stuff. Remember the NodeJS and Bower I mentioned earlier? Yeah, now is the time to use it. Ensure you have NodeJS and NPM installed on your machine. More on that here
We shall install Bower and Yuglify, Pipeline’s default JS and CSS compressor.
To keep things interesting, we shall use the Primer toolkit by Github. It is purposely incomplete and they do say so themselves
Heads up! We love open source, but Primer is unlikely to add new features that are not used in GitHub.com. It's first and foremost our CSS toolkit. We really love to share though, so hopefully that means we're still friends.
Let us proceed to use it in our app, change directory to the static/css folder and run the following command
You should now have a bower_components folder therein with the primer-css and octicons child directories. Time to make changes to the settings file so as to use these assets in our templates.
Change the Templates section of the settings as follows:
Add the following for the STATIC to comply
Great, the settings are looking good, the stars should be aligned, time to collect our compressed static.
The output is long but at the bottom, you should have something similar to this at the bottom:
We shall now proceed to use the static in our templates. Change directory to the templates folder and create a base.html file. Mine looks something like this:
Please note that we include {% load pipeline %} at the top and then use {% stylesheet ‘yokogao’ %} for the output stylesheet.
We shall proceed to create the templates folder and the index.html file for the app within the yokogao folder.
Before we can show data in the index.html file, we need to get the context data within the views file. We are going to use the dateutil module as it provides powerful extensions to the standard datetime module, available in Python. We shall need it to parse the time string returned for created_at into a datetime object. To install dateutil, use pip:
And make changes to our yokogao/views.py file.
Then edit the index.html file:
Now if you run your server, you should have the browser showing your yokogao on the index page.
Next steps
In the next tutorial, we shall add a form to post any Github username and return similar informaton, explore options with an asynchronous REST API, and host our app on Heroku. Thanks for taking the time to read and follow through with this. And many thanks to my colleague Peter Coward for reading through and refactoring my code so it would be palatable. Until next time.