Django (18)


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 […]




Django Custom Queryset vs Custom Manager for Database queries

Note: this applies to Django >= 1.7 From the docs: A Manager is the interface through which database query operations are provided to Django models. At least one Manager exists for every model in a Django application. There are two reasons you might want to customize a Manager: to add extra Manager methods, and/or to […]




Adding Custom User model in Django – part 1

One of the first things I do after creating a new Django or Django Rest Framework project is to create a custom User model. Part 1 deals with case when you add custom User model right at the start of a project. In part 2, we deal with scenario when custom User model is added […]




Writing Custom Django Management Commands

Any command given with manage.py is called management command. Django comes with many built-in management commands like runserver, startapp etc. To see a full list of built-in management commands, type: python manage.py help The benefit of management command script is that this script executes within Django environment. You can run all of Django ORM queries […]




Logging in Django and Django Rest Framework projects

Logging is extremely necessary, during development and production. There are two ways to implement logging. In apps with high scale, we log to console and then have some in-the-cloud solution like Sentry to log the messages from the console. This is great because large scale apps often run on multiple servers and we can converge […]




Django Rest Framework project setup template

Ian Schneider This is a list of what I do when I setup a new DRF project. I am assuming that pipenv is already installed. Also, assuming that Python 3.5+ already installed in the base environment. For those in Python<3.5, please refer here for options. create new environment: pipenv shell pipenv install django djangorestframework markdown […]