Create Login Page for your Django app

Posted on Tue 09 May 2017 in Django • Tagged with django, python, formsLeave a comment

Create Login Page for your Django app

Why Another Post about this?

I found each time I start new Django project, I need to create Login HTML page, and with that I need to write Django View and Form. Each time I tried to Google how to do it, I found many posts about how to do it from scratch, yep as you read, posts telling you how to build a Django View and Form for login page. And there are already built-in Django login View and Form (please check links). So I decided to write my own recipe on how to create Login page in your Django app using the built-in view and form to save time and stop reinvent the wheel.

How to do it?

Actually it’s an easy one, because we’ll use already built-in View and Form, so we will build the Template for it, that it. We have a Form and View ready, so first thing first, we’ll write a template to show HTML form for login, I'm assuming you have another template called base.html and we’ll just write the container block part only, and we are using Boostrap framework and crispy_form app.


{% extends "base.html …
Continue reading

Custom Client Side Validation for Parsley in Django

Posted on Sun 16 April 2017 in Django • Tagged with django, python, parsley, validationLeave a comment

Custom Validation for Parsley in Django

I wrote a post on how to add client side validation for Django Forms. In this post I’ll show you how to add custom client validation to Django Forms. I mean by custom validation is a validation that isn't available in django-parsley, like username availability, password strength, email duplication, etc.. let’s see how to add custom client side validation to Django Form.

Parsley remote validation

Parsley library has remote validation, it's calling AJAX service (Django view) and check if AJAX call returns 2xx HTTP status codes then it's valid input else it's invalid input and shows error message. Thankfully you can use remote validation in Django Form using django-parsley via parsley-extras attribute.


class UserRegistrationForm(forms.ModelForm):
    class Meta:
        parsley_extras = {
            'password': {
                'remote': reverse_lazy('validate-password-parsley'),
                'remote-message': "Password is invalid"
            'username': {
                'remote': reverse_lazy('validate_username_uniqueness'),
                'remote-message': "User with this username is already exists."
            'email': {
                'remote': reverse_lazy('validate_email_uniqueness'),
                'remote-message': "User with this email is already exists."

In the code above we are adding client custom validation as remote to password, username, and email fields, Did you notice that remote key is holding a URL to Django View? let’s create a complete sample to get our head around it.

Django Password …

Continue reading

Client side validation for Django Forms

Posted on Wed 08 March 2017 in Django • Tagged with Django, FormsLeave a comment



In DUTH 2016 there was a session for Django Validation by Loïc Bistuer, in the session Loïc showed many areas you can validate user inputs from Django Form way to the Database Engine. To be honest validation in Django is a piece of cake and even if there is special validation rules you want to implement, you can still implement it, as I said it is easy and straight forward in Django, all of these validation is server side which happen on web server not on client-side, so what about client side?

What is client side validation?

In simple way, client side validation is validation user input on browser before sending the data to sever.

It is good practice to validate user inputs from client side to save roundtrips to server, but it needs extra work to implement your validations using JavaScript or using HTML5 form validation, but there is a way to replicate Django form validation to client side, let’s see how.

Django Parsley

Django Parsley is a Django app that replicate Django Form validation to client side using ParsleyJS library, and it’s so easy to use it.

How to use Django Parsley?

  • Install Django Parsley …

Continue reading

Yet another and easier way to daemonize Celery

Posted on Mon 21 November 2016 in Django • Tagged with django, python, celeryLeave a comment

I wrote a post about how to run celery as daemon and it has many steps to do, many files to copy to your server, configure permissions, and configure upstart, but after sometime I found another and easier way to daemonize Celery.

In this method we will use supervisor to daemonize Celery, and this can be achieved by 3 simple steps, so let's do it.

Step 1

Install Supervisord sudo apt-get install supervisor on Ubuntu server. For other OSs please visit the official documentation

Step 2

This is the most important step, and in this step we will create supervisor configuration file to daemonize Celery.

Supervisor Configuration File

Celery project provide the basic configuration file in their Github repository.

Supervisor configuration files live in \etc\supervisor\conf\

Celery official configuration file

; ==================================
;  celery worker supervisor example
; ==================================

; Set full path to celery program if using virtualenv
command=celery worker -A proj --loglevel=INFO


; Need to wait for currently executing tasks to finish at shutdown.
; Increase this if you have very long running tasks.
stopwaitsecs …
Continue reading

Add isort and flake8 test to your Django Project

Posted on Tue 27 September 2016 in Django • Tagged with django, python, unittest, howtoLeave a comment

Unit Test Works

In this post I'll tell you how to add isort and flake8 tests to your Django project test suite, this is simple script and you can start from this point and customize you own test suite.

As they said Django is a “batteries included” web application framework, and one of the batteries is Testing framework, testing in Django is easy and you can start writing tests even if you didn't do it before.

If you ever work with Django, you will write tests for your project, to test your views, models, forms, etc.. but it's better if you can add isort and flake8 tests to maintain the quality of your Python code.


We need some packages first so we can run the tests

pip install flake8 isort flake8-isort


It's better to create and use sperate virtualenv


flake8-isort is using .isort.cfg file to run the isort test against it, so let's add our configuration file.

.isort.cfg file

max_line_length = 120
indent_style = space
indent_size = 4
known_first_party = isort
ignore_frosted_errors = E103
skip =,build,.tox
balanced_wrapping = true
not_skip =

Test Python Script

Now it's time to write the Python script to run Django, isort, & flake8 tests.

runtests …

Continue reading

How to run Celery as daemon?

Posted on Tue 20 September 2016 in Django • Tagged with django, python, celeryLeave a comment

celery smoothy

Install upstart script


We need to make the user which will be used to run celery an owner of log and run directories.

sudo chown -R root:root /var/log/celery/
sudo chown -R root:root /var/run/celery/

Celery upstart

1- Download celeryd file from Celery official github repository.

2- Copy celeryd to /etc/init.d/celeryd

sudo cp celeryd /etc/init.d/

3- Make it executable

sudo chmod +x celeryd

4- Make it run on startup

sudo update-rc.d celeryd defaults
sudo update-rc.d celeryd enable

5- make it owned by root

sudo chown root:root celeryd

6- Done, now you can start/stop/restart celeryd as service

sudo service celeryd start
sudo service celeryd stop
sudo service celeryd restart

Celerybeat upstart

1- Download celerybeat file from Celery official github repository.

2- Copy celerybeat to /etc/init.d/celerybeat

sudo cp celerybeat /etc/init.d/

3- Make it executable sudo chmod +x celerybeat

4- Make it run on startup

sudo update-rc.d celerybeat defaults
sudo update-rc.d celerybeat enable

5- make it owned by root

sudo chown root:root celerybeat

6- Done, now you can start/stop/restart celerd as service

sudo service celerybeat start
sudo service …
Continue reading

Send Emails Asynchronously from Django

Posted on Sun 24 January 2016 in Django • Tagged with django, python, celery, emailsLeave a comment

In this article you will learn:

  1. How to send emails from Django.
  2. How to use Celery and Django-Celery-Email to send email asynchronously.

Last week I had the chance to get my hand dirty with Celery, I used it to send emails from Django project asynchronously using Django app call djnago-celery-email. What am I mean by sending emails asynchronously? I mean that user doesn’t wait for SMTP/mail server to send email message and give feedback to the web server, instead Django will call the send email procedure, pass it to Celery to do it away from Django application, so that user won’t wait, for example you want to send email to customer once he save customer information, you don’t want to keep user wait for application to save the customer data and send email, by sending email asynchronously user will only wait for saving confirmation and sending email celery will take care of it in the background.

Alt Text

Now let’s see how we can do that.

Step 1: Install and Setup Celery

  • In terminal, active your virtualenv and type $ pip install django-celery
  • Now celery and its Django app djcelery are installed into your virtualenv.
  • Next step is …
Continue reading

Extend Django User Model and Generic Class Based View "GCBV"

Posted on Fri 18 December 2015 in Django • Tagged with Django, PythonLeave a comment

1450356512_full.png When I started to learn Django, I used to use the function based view aka FBV and in my current project I decided to learn class based view CBV, I watched one DjangoCon videos by Andrew Pinkham to make this easier on me, and if you tried or planning to learn CBV, you will be confused about the class based views and the generic class based views inside Django, it’s some many of them, please watch the video to get your head around it. OK, now I’ve done my homework and it’s time to use CBV, believe me it’s easy and you will find the number of code lines inside our views will be decreased specially if you use GCBV. User Model and GCBV

What is the relation between GCBV and User Model?

Great question, while I’m working with one of my models fro example her the Teacher model, and teacher will has a user credentials in order to user the app. The easiest and the straightforward way is to make one-to-one relation with django.contrib.auth.models.User please read the quote from Django documentation:

There are two ways to extend the default User …

Continue reading

Free Django hosting

Posted on Sun 19 April 2015 in Django • Tagged with django, pythonLeave a comment

If you developed Django application and want to deploy/publish it to the web you can check this list of Django friendly (, but if you want a free hosting you can use Python Anywhere free hosting, I’m using it for my Django app and I’m very comfortable with but before using the free hosting please consider the following:

  1. They provide only MySQL for free hosting, so if you developed your app using PostgreSQL or SQLite, please download the correct driver.
  2. Application URL must be sub-domain of, example
  3. I’ll host one application only.
  4. No full access to a machine, you’ll deploy your application using console, use the console to install your virtual environment and packages, and edit some code using their in-browser code editor.

How to deploy?

  1. First go to this link and register your free account, then 
  2. Go to your dashboard, and select Consoles tab.
  3. Click on Python 2.7 (I personally still using python 2.7 but if you want select python 3.3) to open a new console.
  4. New console will open in your browser.
  5. Start by creating your virtualenv and install the required packages …
Continue reading

My First Django App

Posted on Mon 16 March 2015 in Django • Tagged with django, python, sideprojectLeave a comment

I'm starting to use Django and Python to develop web applications, I really love Django and during my learning process I thought I can build something while learning, and I had one idea keep it on hold as side project so I decided it's time to build it.

The Idea

Me and my wife have our financial system which is dividing the monthly budget into a weekly budget, most of us make budget on monthly basis because we get paid on monthly basis, but imagine your budget will be on weekly basis, it’s like every week is new budget and new start, and in order to keep track of the weekly budget you need to know how much you need to spend in a week. To be honest in the first we made a lot of mistakes but at the end we found out how much we need to spend weekly, and this need analysis with trial and error for at least one month then you will master your weekly budgeting.

Now the idea is build a system that can keep track of your payments or expenses against your estimated budget for a week. The application name is Ma3ana …

Continue reading