Aug 1, 2018

Change password of a user in Django

Start by opening the Django shell from your terminal

> python shell

Then inside the Django shell, first get hold of that user object, then change the password and save.

Jan 19, 2017

Django HTTP only CSRF cookie

Django sets a CSRF cookie to protect against cross site request forgeries. By default this cookie is set to be accessible to JavaScript on your web page. That means, the cookie will be accessible to a malicious script running on this page, too. ...

Dec 21, 2016

Django query time using the connection module

The Django connection module keeps track of recent queries. To see the SQL code and time taken to run the queries, have a look at the connection.queries field.

from django.db import connec...
Dec 13, 2016

Reading list of values from django request object

Sometimes an array of values is sent as a param of a GET or POST request to the server. For example, when you have a multiple select or a group of check boxes in the front end, the form submission URL may look like this:

Use getlist to r...

Nov 28, 2016

Django F expression for increment

Django F expressions can be used to manipulate object fields in the database without pulling them out to the application layer. An use case is when a field needs to be incremented. The less then ideal way to do so wou...

Jan 27, 2016

Configuring unicode in Gunicorn upstart

Gunicorn is a good tool for deploying django websites on Ubuntu (and other Linux) servers. However, servers are often not configured to support unicode by default. Some environmental variables have to be set in order to enable unicode handling. Set these environmental variables in the upstart fil...

Jan 19, 2016

Calling C functions in Python scripts

Python is an extensible language. Code written in compiled languages like C can be invoked from Python with a little effort. This allows software developers to write simple human readable scripts in Python to make use of faster libraries written in compiled languages.

Say, we have the followin...

Jan 19, 2016

Static member in Python

For creating a static field of a class, write it inside the class outside of the scope of any member functions. To declare a variable belonging to instance, add self. prefix. To make a function static, add the @staticmethod decorator.

class Some_class(Name_of_super_class):

Dec 24, 2015

Don't sudo pip install in virtual environments

Deploying python projects in virutal environemnts is a fairly standard practice. Such a deployment process could include a step to install all required python modules from a requirement file like so:

(your_virtual_env)> pip install -r requirements.txt

Does it seem a bit odd that we didn't ...

Oct 6, 2014

Debugging RE

One of Python's most useful feature is its Regular Expression support. However, it is easy to get a regular expression wrong and if you do, debugging it can be a painful as well. Fortunately, Python allows us to set a DEBUG flag during compilation of a regular expression.

import re
pat = re...
Sep 23, 2014

A word of caution on os.path.abspath

Due to the implementations of abspath in Python, it requires some careful consideration. The source code for os.path.abspath can be found in the file in your Python installation folder. Windows users open instead. Have a look at the abspath function (I'm quoting what I ...

Sep 4, 2014

Python object from dictionary

It is more convenient to access the fields of a class using the dot notation than accessing the entries in a dictionary. Python allows a quick conversion between simple dictionaries to python objects.

x = myObj.x
x = myDict['x']

A python object can be instantiated from the contents of a ...

Feb 8, 2014

String representation of Python objects

It is often necessary to provide string representation of objects. Application of this can be found in debugging, storing in a file, communication across the network, etc. Two builtin functions str() and repr() output string representation of Python objects. But how do they differ from each other...

Jan 1, 2014

Python for Scientific Computing

Scientific computing requires specialized programming languages and ecosystems of libraries/APIs. General purpose programming languages do get the job done, but a specialized ecosystem and language allows the scientist to be the scientist more often than being the programmer. Or better yet, to fi...