Gunicorn with Django

While searching for deploying the Django app, I found something called Gunicorn.
I tried:

gunicorn sage.wsgi

where sage is the name of the Django project. And now the django app can be accessed from the default address.

To make it automatic, I had to do some tweaks like creating file in /etc/init and creating its symlink in /etc/init.d/ etc.

Here is the file civiloctave.conf in the folder /etc/init :

description "civiloctave"
start on runlevel [2345]
stop on runlevel [06]
respawn limit 10 5

LOGDIR=$(dirname $LOGFILE)
test -d $LOGDIR || mkdir -p $LOGDIR
cd /home/mandeep/CivilOctave/$NAME
gunicorn sage.wsgi

exec /home/mandeep/CivilOctave/$NAME/ \
--user=$USER --group=$GROUP --log-level=debug \
--name=$NAME -b$PORT \
--log-file=$LOGFILE 2>>$LOGFILE
end script


After creating its symlink in /etc/init.d/ and rebooting, the process starts itself and then we can open the address (i.e. localhost:8000) anytime in the browser to see it working. I couldn’t get it working through apache. I’ll get on that later on.


Jquery Datepicker

For using jquery datepicker in a django form, I included four external files:

<link rel="stylesheet" href="">

<script src=""></script>

<script src=""></script>

<link rel="stylesheet" href="">

in the base.html and then added a script

$(function() {
$( "#id_date" ).datepicker();
$(function() {
$( "#id_hall" ).selectmenu()
.selectmenu( "menuWidget" )
.addClass( "overflow" );

So on viewing the book.html page (that contains the form), I viewed its source and found id of the date input field(#id_date) and then assigned it accordingly in the script above. In the script, I also added input id of hall(#id_hall) to show it as a nice looking dropdown list.

Also added some styles:

fieldset {
border: 0;
label, label#id_start_time{
display: block;
margin: 10px 0 0 0;
select {
width: 200px;

.overflow {
height: auto;

The commit can be seen @
I’d also downloaded the jquery files and css in the static folder but it didn’t work may be because of some path error.

Booking Restriction and datepicker

As in the last post I specified the booking was working but if we book the same hall for start and end time then it successfully added to the database as a booking, which should not be there. Hence to restrict them I found a solution of unique_together.

To use this we need to specify which fields we need to be unique together means we don’t need any entry(booking) to be there with the same hall, start time, end time. Hence to accomplish this, I added in the under the Booking model:

class Meta:
     unique_together = (‘hall’, ‘start_time’, ‘end_time’,)

Previously we have to input date in a character field, but now I used a widget.  Using this there are drop downs for selecting date, month and year.

from .models import Booking
from django.contrib.admin import widgets
from django.forms.extras.widgets import SelectDateWidget
a = (‘2015’, ‘2016’, ‘2017’)

class BookingForm(forms.ModelForm):
    date = forms.DateField(widget=SelectDateWidget(years=a))

I imported the SelectDateWidget from Django widgets and then used it in the date field as shown in the last line above. There I passed a list ‘a’ as the years. This results into only 3 entries(2015,2016,2017) in the year choice field when we view the html(book.html).

Working Booking

As Raman started working on User Authentication, I decided to work on the Bookings. In home/ I created a BookingForm class which was linked to the ‘Booking’ model using model = Booking

As you can see in the commit:

In the class meta: , there are fields specified which will be shown on the html for the input. This input will then be stored in the database. So we call this form in the book.html page using {{ form.as_p }} and then in the, I called the template book.html which contains the form (BookingForm). There is a validation applied that if form is valid only then save it to the database else show the errors.

Admin Panel List Display

This is some of part of file:

class BookingAdmin(admin.ModelAdmin):
list_display = [‘hall’, ‘event_name’, ‘name’, ‘date’, ‘start_time’, ‘end_time’, ’email’, ‘status’ ]
class Meta:
model = Booking, BookingAdmin)

In this file, there is a new ModelAdmin class named “BookingAdmin”. It is connected to the model “Booking” that we created in the file. Hence it did show the name of the hall only previously in the admin panel.

But we need all the details of the booking like name of event, time, date, which hall, organizer details etc. Then it will be easy for the admin to decide which booking to select. So the meaning of second line is this: it displays the fields accordingly as listed in the single quotes.

It will only take effect after we register by writing the last line. It registers the model Booking and module BookingAdmin together so that we can view model’s information in the admin panel UI.

Similarly I used the list_display method for Hall model to show the name of the hall and no. of seats when overviewing the list.



How to remove superuser from Django Project

Open Terminal(Ctrl+Alt+T).
Browse to your project source directory(where '' exists):
$ cd project

Note: Replace 'project' with your directory name.

Now execute these commands:
$ python shell

Then the interactive python shell with django powers will appear. Then type in:
$ from django.contrib.auth.models import User
$ User.objects.get(username="NameHere", is_superuser=True).delete()

You should replace the name of the superuser you created with NameHere in the last command.