How-to Create A Domain Alias In Your Django Development Environment

Close-up of a dark pink/purple tint computer keyboard

Quickly Setup domain routing for multiple Django sites

Overview

Recently, while working on a new Django project I need to create two Django sites that referred to each other. One would be the main domain i.e. mysite.com and the other would be a subdomain i.e. hello.mysite.com.

So I started wondering if there was a way to test this locally in my development environment, and after 2 days of searching online I really found nothing and started to think it was impossible.

However, after speaking with a good friend of mine, Chaz Hill, who happens to be a really kick-ass System Administrator he pointed me in the right direction. I was trying to do all this with Vagrant at the time but in this post I’ll show you how you can do it the normal way, through the shell.

Configure Django

Create your Django project and app, then launch the Django development server using port 80, this is the important part (even though I’ve been using Django for sometime now, it totally slipped my mine I could set the port to 80).

Fix Permissions Error

Now if you tried running this without sudo – you will get an permissions error, the fix of course is to do this => sudo python manage.py runserver 127.0.0.1:80 

Pic of Django development server running in shell and in the foreground the hosts file.

Pic of Django development server running in shell and in the foreground the hosts file.

Run Django Development Server

With the Django development server running, open your hosts file, /etc/hosts and add 120.0.0.1 mysite.com – save the file and goto your browser and type mysite.com, you’ll happily see your Django site running ?

How-to Use Python To Create A Beautiful Web Calendar

Python Web Calendar

Overview

At work I usually use PHP, HTML, CSS and JavaScript to build any bits of functionality but sometimes it can take quite awhile to get the what I want working.

So recently, I’ve found it really nice to use Python in my workflow to “glue” together certain aspects of a project.

For example I found it easier to create a employee pay day calendar with Python vs creating it in PHP. Not only was it fun to build but I’m left with more time to explore extending my initial idea.

 

What it should look like

Python Payday Web Calendar

The pay day calendar generated with Python

 

  1. Subclass The Python HTMLCalendar Module
  2. Pay days data
  3. Generate the webpage
  4. Presentation
  5. Download Files/Code

 

 

Subclass The Python HTMLCalendar Module

Just make it easy, just subclass. In this case I made a subclass of HTMLCalendar from the Calendar module and overrode some of the methods.

Python Web Calendar

PayDayCalendar Class, the highlighted area shows where to add in some CSS into the HTML

 

The method, formatyearpage() was the main method that needed to be modified because I wanted to dump the returned text into an actual HTML document.

So I simple added the ability to write the text to a HTML file. The remaining methods were only necessary for adding CSS attributes to the HTML.

 

Python Web Calendar

PayDayCalendar Class, from line 98 to 100 is the simple function call to write the text to a HTML file

 

Pay days data

The main script, generatePayDayWebpage.py generates the calendar and saves it to a file called CIGpaydays.html.

This script is quite simple and only contains the imported subclass of HTMLCalendar, called PayDayCalendar.

This class takes a list of tuples containing, the month and the day.

Python Web Calendar

The main script, generatePayDayWebpage.py, clean and simple

 

Generate the webpage

After creating the subclass and the main script it was time to see if it all paid off.

Running generatePayDayWebpage.py in terminal produced the desired results, a print out of the data, before and after going to PayDayCalendar class along with a confirmation that the webpage had been generated.

 

Python Web Calendar

Running generatePayDayWebpage.py in terminal, showing debug print-out and confirmation

 

 

Presentation

Now that I had generated the HTML calendar all I had to do now was style it. The ‘4’ that was passed to formatyearpage() was a parameter that lays out the calendar in 4 columns, so there wasn’t too to do in terms of CSS, only basic styling of colors and fonts.

Python Web Calendar

 

Python Web Calendar

 

Python Web Calendar

 

NOTE: this version of the calendar uses tables and in the future I intend on changing it to tableless layout.

 

 

Conclusion

Thanks for taking the time to read this article. Please feel free to leave a message or comment with your thoughts.