Django

Serving Django static files in shared hosting

I kept my django project files out of public_html. You probably know the reason – to prevent direct access to those folder/scripts. Since the static files aren’t inside “public_html” we need to provide some way to access them. Consider the following 1. The project ‘myapp’ is in /home1/myuser/django/ 2. ‘static’ folder is in myapp/static 3. […]

A dynamic template filter for django forms

I used to assign attribute to form controls like this: But this just doesn’t feel right. Wouldn’t it be so much better if we could do this in the template and may be by writing less code? Okay, here’s my solution: file: forms_attr.py put this file in templatetags folder of your app. And then in […]

Migrating from Django template to Jinja2

Jinja2 is great. Far better than the django template I think. But I’m not here to talk about that. My guess is you already decided to use Jinja2. So let’s talk about the changes you need. 1. You can use Jingo to set it up easily. install: Create a file e.g. ‘jingo_settings.py’ & paste the […]

DJango ORM: Sum of multiplication

Consider the following model: Now to calculate sum of progress*estimated_days follow this: Note: if two fields are of different types, say integer & float, the type you want return should be passed as the first parameter of Sum.

var_dump for python

var_dump is a PHP’s var_dump() equivalent function for python. It displays structured information such as type, value etc of a python object, list, tuple, dict & other types. Installation using pip Or clone the project & cd into the `python-var_dump` directory then run: Examples Example #1: Example #2: you can pass more than one argument: […]

Custom validation in Django Admin list view

In my previous post, I explained how to apply Custom validation in Django admin form. But we can also make one or more field editable in the list view. In this post I’m going to explain how to apply custom validation in list view. We’ll just modify the CountryAdmin class described in the previous post. […]

Custom validation in Django admin form

Django applies the validation rules defined in the Model or Form. But sometimes we might need custom validation in Django admin forms. Let’s assume that we are developing an app that’ll store all of the countries data. We’ll also feature 5 of them in the homepage of the app. Let’s consider the following Model file: […]

Textarea in DJango admin form

For CharField Django admin automatically generates input html block. But if we need to make a CharField appear as textarea in html we can do so by creating a Form class for the model. Consider the following model: file: models.py Next we create the form & add it to ModelAdmin subclass file: admin.py

Serving static files (CSS, js, images) in Django, the best way

The Problem: Django tells us to arrange app specific static files in development environment, that is, we are supposed to create a folder named static inside every app folder of INSTALLED_APPS and keep the static files needed for that app inside that folder. But is it really practical? I mean who the hell would put […]