Finance Project 4: Finally getting started


Now that I’ve worked out how I’m going to structure my data I need to work out how to store it.

That’s a really simple decision as it’s largely driven by platform. If I was developing on Windows I’d pick a version of MS SQL. If I was a large enterprise using Java with stupid amounts of money to burn, I might pick Oracle. I’m developing on Linux, so it comes down to MySQL and Postgres… sort of…

The twist is that I’ve decided to use Django as a framework, which means that I won’t be interacting directly with the database. Django will take care of mapping code objects to database tables.

That might seem like a bit of a cheat, given that I’ve used Django before and this whole project is supposed to be about learning new things.

That’s true, but it’s also true to say that I only scratched the surface with Django. My current thinking is to bootstrap the basic functionality in Django so that I can quickly get basic data entry forms up and running using the framework and templates. Once I have the basics, I can then work on the more advanced front end functionality with existing data in the system. I could even end up replacing the Django forms with the Django REST framework and using api calls from a Javascript front end.

That should also make it possible to get something running within the next few weeks. It would be good to start entering real data from the start of 2019, even if the data entry is a bit clunkier than it will be in the final system.

My development version will use the default SQLite database, and then I’ll move it across to my “production” MySQL database.

There are a few steps to getting Django up and running. I won’t go into details, as I’d be repeating the tutorial, but here’s how I’ve done it. First of all, I have a project called “home” that runs my existing Django apps, so I replicated that in my dev environment. I’m running on CentOS 7 with the SCL version of Python 3.6, so the first thing I needed to do is enable it:

$ scl enable rh-python36 bash
$ mkdir django-dev
$ cd django-dev
$ python -m virtualenv venv
$ source ./venv/bin/activate
$ pip install Django
$ django-admin startproject home
$ cd home
$ python manage.py startapp finance

Then I needed to make some changes to settings.py:

ALLOWED_HOSTS = [my_server_ip]
LANGUAGE_CODE = 'en_gb'
TIME_ZONE = 'Europe/London'

I’ve added my server’s IP address to ALLOWED_HOSTS, as I’ll be accessing it from another machine using the IP address in the URL. The production version uses a dynamic DNS hostname, but for development the IP address will do.

Finally, I set up the basic URL mapping. In the home/urls.py file:

from django.contrib import admin
from django.urls import include, path

urlpatterns = [
   path('admin/', admin.site.urls),
   path('finance/', include('finance.urls', namespace="finance"))
]

Note that when I refer to files, it will always be relative to the project’s home directory – as I’ve set it up, that will be ~/django-dev/home.

And in finance/urls.py:

from django.urls import path

from . import views

app_name = 'finance'

urlpatterns = [
   path('', views.index, name='index'),
]

And finally a dummy index page:

from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello, world. You're at the finance index.")

I can now run the development server:

$ cd home
$ python manage.py runserver 0.0.0.0:8001

I’m running it on all interfaces (0:0:0:0) so that I can access it remotely, and on port 8001 as the server is already running my production instance on port 8000.

Now that I’ve got the basic Django infrastructure running, I’m ready to start adding the data models.

in Finance Project

Related Posts

Add a Comment

Your email address will not be published. All comments will be reviewed.