Python/Django – Avoid saving passwords in source code

Although I wasn’t able to come across anything Python-specific on stackoverflow, I did find a website that was helpful, and thought I’d share the solution with the rest of the community. The solution: environment variables. Note: Although environment variables are similar in both Linux/Unix/OS X and in the Windows worlds, I haven’t tested this code … Read more

Django setting : psycopg2.OperationalError: FATAL: Peer authentication failed for user “indivo”

I have similar problem and solved it with this answer by adding localhost to the database HOST settings in settings.py, so your database settings should look like this: DATABASES = { ‘default’:{ ‘ENGINE’:’django.db.backends.postgresql_psycopg2′, # ‘.postgresql_psycopg2’, ‘.mysql’, or ‘.oracle’ ‘NAME’:’indivo’, # Required to be non-empty string ‘USER’:’indivo’, # Required to be non-empty string ‘PASSWORD’:’ritvik’, ‘HOST’:’localhost’, # … Read more

Get absolute path of django app

Python modules (including Django apps) have a __file__ attribute that tells you the location of their __init__.py file on the filesystem, so import appname pth = os.path.dirname(appname.__file__) should do what you want. In usual circumstances, os.path.absname(appname.__path__[0]), but it’s possible for apps to change that if they want to import files in a weird way. (I … Read more

how to check DEBUG true/false in django template – exactly in layout.html [duplicate]

In newer versions of Django it is possible just by specifying INTERNAL_IPS in settings. For example: INTERNAL_IPS = ( ‘127.0.0.1’, ‘192.168.1.23’, ) and then in template just: {% if debug %} because context processors responsible for that by default, and the answers from How to check the TEMPLATE_DEBUG flag in a django template? are bit … Read more

Django ALLOWED_HOSTS IPs range

No, this is not currently possible. According to the docs, the following syntax is supported: [‘www.example.com’] # Fully qualified domain [‘.example.com’] # Subdomain wildcard, matches example.com and www.example.com [‘*’] # Matches anything If you look at the implementation of the validate_host method, you can see that using ‘*’ by itself is allowed, but using * … Read more

django settings per application – best practice?

The simplest solution is to use the getattr(settings, ‘MY_SETTING’, ‘my_default’) trick that you mention youself. It can become a bit tedious to have to do this in multiple places, though. Extra recommendation: use a per-app prefix like MYAPP_MY_SETTING. There is a django app, however, that gets rid of the getattr and that handles the prefix … Read more