python-django-logo

How to separate your settings.py file for different environments in Django projects.

This is another practice that I like to implement in my Django projects, it is a safe way to have many apps and customized variables according your project environments (local development, testing, integration, production, etc).

Step 1: Create a directory inside your project folder named settings, and create a __init__.py file inside it.

- manage.py
- your_project
-- settings
--- __init__.py
-- __init__.py
-- settings.py
-- urls.py
-- wsgi.py

Step 2: Rename your current settings.py file to base.py and move inside settings (new folder from step 1)

- manage.py
- your_project
-- settings
--- __init__.py
--- base.py
-- __init__.py
-- urls.py
-- wsgi.py

Step 3: Create local.py file (e.g) to use it in your local environment, and then import everything from base:

- manage.py
- your_project
-- settings
--- __init__.py
--- base.py
--- local.py
-- __init__.py
-- urls.py
-- wsgi.py
# settings/local.py
from .base import *

Step 4: Add changes and remove variables from base.py according to your requirements (e.g).

# settings/local.py
from .base import *

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

Step 5 (Optional): If you use virtualenvwrapper do not forget to add the following variable to your postactivate file:

export DJANGO_SETTINGS_MODULE=YourProject.settings.local

 Step 6 (Optional): If you have several virtualenv environments in your computer, you should add the following command to your postdeactivate file:

unset DJANGO_SETTINGS_MODULE

 

Now you can add applications according to your requirements, for example just add django-debug-toolbar to local development, etc.

 

image02

How to use aliases and postactivate with virtualenvwrapper to make easier your development tasks.

This is just a TIP, you can follow it or create yours based on it as well.

I like to use virtualenvwrapper for my Django and Python projects. I usually add some alias to postactivate file in order to avoid type a lot of commands with frequent tasks (reset default testing data for example).

So this is quite simple you just need to add aliases according your needs and practices.

Step 1: Locate and open with some text editor your postactivate script:

e.g:

sergio$ vim ~/.virtualenvs/your_project_name/bin/postactivate

Step 2: Add aliases and save:

e.g:

alias code_review='flake8 . --ignore=E501,F403'
alias migrate='rm your_project/db.sqlite3 && python manage.py makemigrations && python manage.py migrate'

Step 3: Activate (or deactivate and activate) your environment to test your aliases:

e.g activate:

sergio$ workon your_project

e.g deactivate and activate

(your_project) sergio$ deactivate
sergio$ workon your_project
Django + git + virtualenvwrapper

How to start a Django project + virtualenvwrapper + Git

Django is a cool python web framework, but it’s even cooler if you take advantage of other complementary tools and get a good environment to start a new project. So this is how I combine virtualenvwrapper + Django + git in a successful way.

These are the steps:

1. Install pip

pip is a python package manager system, is highly recommended for installation, update and management of all python packages inside in a project, there are different ways to install it, it depends of your operating system, so you should find the right way for you.

2. Install virtualenvwrapper

virtualenvwrapper is a set of extensions related to virtualenv, so the big advantage are the wrappers for create and delete environments, it makes easier the workflow.

pip install virtualenvwrapper (easiest way to install it)

After the installation you just need add these three lines in your shell startup file (.bashrc, .profile, etc). I created ~/Code folder before to add the lines

export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/Code
source /usr/local/bin/virtualenvwrapper.sh

After adding those lines, you need to reload the startup file. (e.g source ~/.bashrc)

3. Create the virtual environment for the project

Now, we have virtualenvwrapper ready to use, this line makes a new project

mkproject project_name (replace project_name with the name of your project)

A new virtual environment and a new folder name_of_project will be created, the path of the project will be ~/Code/project_name and the shell will be redirect to that path, you can validate it, if you run pwd command you will see

~/Code/project_name (~ will be replaced with you home directory)

You can use workon, deactivate and other virtualenv commands from now, also you can use other virtualenvwrapper stuff to accomplish productive tasks, follow the virtualenvwrapper documentation for more info.

4. Clone existing repo with Git or create a new one.

Many current projects use Github, Bitbucket or other hosting services, so you can create a new repository on those services and clone it on your new project folder or you can create a new local one. Don’t forget create a .gitignore file.

5. Install Django and create the requirements.txt file

Use pip to install the latest Django package version or the version you want:

pip install django

Create the requirements.txt file

pip freeze > requirements.txt

This requirements.txt file is very useful if you want to replicate the development environment in other computers, you can install the requirements like this:

pip install -r requirements.txt

You can commit the changes

6. Create Django project

To create the django project you just need to use django-admin.py startproject and two parameters: the project name and the location, so inside the ~/Code/project_name you can type:

django-admin.py startproject project_name .

or

django-admin.py startproject project_name ~/Code/project_name

Notice in the first one I’m using a period after the project’s name, which is a way to indicate that I want a new project in my current directory.

 

You can commit the initial project files and that’s enough to start working in a new project with Django web framework.🙂

 

Hello World, I’m starting a new blog in english.

Hi, my name is Sergio Infante, I’m software developer. Recently, I decided to start this new blog in english (By the way my native language is spanish and I have one blog in spanish too), I realised that somehow I really enjoy to write and share technical stuff which are about software development, video games, gadgets, and any kind of tech things that I like.

In these years I have been working in several software development projects which allow me to have many friends, the language we always share is english, so that’s the reason why I’m writing in english now.

I won’t abandon my spanish blog, but probably I’ll write more english posts than spanish ones.🙂

If you want more info about me, you can find it on my about page: http://about.me/neosergio