Blog


Using Pytest with Django and Django Rest Framework

This article is inspired by article here. What is Pytest Pytest is testing framework for Python. Very popular with Django. Killer feature : Fixtures Fixtures are the killer feature of Pytest. Fixtures are functions that run before and after each test, like setUp and tearDown in unitest and labelled pytest killer feature. Fixtures are used […]




Python: Bare asterisk in function arguments?




Adding Custom User model in Django – part 2 (using fixtures)

See part 1 here. In this part we are discussing what to do when we add custom user model later in the project. Note that in this scenario, we are in a very non-ideal situation. To keep unknown surprises at the minimum, we will still delete old database but we will use the concept of […]




Adding AJAX to Python Django projects

A very nice explanation on SO regarding what and why about AJAX from Django perspective: https://stackoverflow.com/questions/20306981/how-do-i-integrate-ajax-with-django-applications So, specifically in Django, when we use AJAX to do a form POST request, we need a CSRF token. This is a security feature. If we are using Django templates, the templates do the job of generating a csrf […]




Steps for new Python library or package

Here are the steps. Create repo on github with license, gitignore and readme stubs. Clone the repo in the work area. Lets say, my new library is called new-library new-library is the repo name. Inside the cloned repo, create virtual env by running: pipenv install –python 3.8 Above step creates a virtualenv using python 3.8 […]




Django sessions for anonymous users

We know that all logged in users are connected to a session. This is something done for you by Django middlewares. Similarly, for anonymous users (not logged in), every time the server receives a request, django creates a session object (meaning, an object with session_key, session_data and expire_data values). But the catch here is that […]




_set meaning in Django ORM relationship

_set is associated with reverse relation on a model. Django allows you to access reverse relations on a model. By default, Django creates a manager (RelatedManager) on your model to handle this, named <model>_set, where <model> is your model name in lowercase. Excellent link on StackOverflow here: https://stackoverflow.com/questions/25386119/whats-the-difference-between-a-onetoone-manytomany-and-a-foreignkey-field-in-d If we have these models:

In […]




Setting active navbar link in Django template

Here is probably the best way to set the active navbar link in Django template. Note, this needs no jQuery/ javascript. Step 1 Create named urls:

Step 2 Create a Navbar template. I keep it stored in a file called navbar.html inside “templates/base” folder. This “templates” folder is at same level as manage.py I […]




Adding login and registration in Django

Adding login and registration is very straight forward in Django. Note that Django provides a built in User model. User model exists ONLY for authentication. Use the User model to only store absolutely necessary info for authentication. Like Username and password (or if using custom user model, email and password.. I usually make the email […]




Adding reverse url lookup in Django

Reverse url lookup is very cool feature in Django that allows us to not hard-code urls in templates or controller logic. This helps us change the URLs later in urls.py and not have to make the same changes everywhere in templates and logic. Keep it DRY. To implement reverse url lookup, you need to do […]