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
respawn limit 10 5

script
NAME=sage
PORT=8000
NUM_WORKERS=2
TIMEOUT=120
USER=mandeep
GROUP=www-data
LOGFILE=/var/log/gunicorn/$NAME.log
LOGDIR=$(dirname $LOGFILE)
test -d $LOGDIR || mkdir -p $LOGDIR
cd /home/mandeep/CivilOctave/$NAME
gunicorn sage.wsgi


exec /home/mandeep/CivilOctave/$NAME/manage.py \
-w $NUM_WORKERS -t $TIMEOUT \
--user=$USER --group=$GROUP --log-level=debug \
--name=$NAME -b 127.0.0.1:$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.

Advertisements

Jquery Datepicker


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

<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.3/themes/smoothness/jquery-ui.css">

<script src="http://code.jquery.com/jquery-1.10.2.js"></script>

<script src="http://code.jquery.com/ui/1.11.3/jquery-ui.js"></script>

<link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">

in the base.html and then added a script

<script>
$(function() {
$( "#id_date" ).datepicker();
});
</script>
<script>
$(function() {
$( "#id_hall" ).selectmenu()
.selectmenu( "menuWidget" )
.addClass( "overflow" );
});
</script>

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:

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

}
.overflow {
height: auto;
}
</style>

The commit can be seen @ https://github.com/rvirdiz/booking_system/commit/c9e65898170af181915f1e53952c5a1037ca01cf
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 models.py 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.

In forms.py,
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/forms.py I created a BookingForm class which was linked to the ‘Booking’ model using model = Booking

As you can see in the commit: https://github.com/rvirdiz/booking_system/commit/fc48cd1cc9b41ff953bd265236a040f4cfc3f848

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 views.py, 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 admin.py file:

class BookingAdmin(admin.ModelAdmin):
list_display = [‘hall’, ‘event_name’, ‘name’, ‘date’, ‘start_time’, ‘end_time’, ’email’, ‘status’ ]
class Meta:
model = Booking
admin.site.register(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 models.py 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 'manage.py' exists):
$ cd project

Note: Replace 'project' with your directory name.

Now execute these commands:
$ python manage.py 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.