Set up psql database in django.

Share Button

I use sqlite as my django database, but there are some limitations in sqlite. So I want to switch to mysql or psql(PostgreSQL). Both of them need a little bit more setup than sqlite, but still quick easy.

  1. Install database client.
  2. Create user.
  3. Create database.
  4. Modify django settings.
Let’s begin with mysql.

Now let’s see how to do it for psql.

Ok now we created user name, password and database, we need to modify settings.py accordingly.

We are all set!

Editing Python code in Vim.

Share Button

Python is relied on indentation, and if you edit python code in Vim, you may found it will mess up your indentation. Here is a simple way to fix it.

Open your file in Vim you will see something like this:

and enter Vim command

to see all white spaces. If you see something like this:

You know you have inconsistent indentation. “^I” means tab, but “print c” statement is indented by space.

How to fix this? Easy.

  1. Edit your .vimrc file to have following:
  2. For the files having indentation issue, run Vim command to fix them:
  3. Now check the file again using “:set list”, and you will see all tabs are gone. They are places by spaces.

Serve django static files for production service and solve missing admin page css problem.

Share Button

It works perfectly on dev version of my django site. But when I deploy it to production (served by apache), I realized that css is missing for admin pages. Then I digged a little deeper to see what I could do to solve this issue. It actually quite simple. There are two files you need to modify.

  1. Modify settings.py.

    here STATIC_ROOT will be the folder where django will copy all admin static files. STATIC_URL will corresponding to the alias name in your apache config file. Don’t worry, keep reading and it will be clear to you.
  2. Go to your django site directory and run

    This will copy all static files including admin css, image, js files into your STATIC_ROOT. Now check your “static” folder, you should see “admin” folder which has “css”, “img” and “js” sub-folders.
  3. Modify corresponding apache config files under /etc/apache2/sites-available/ folder.

    where “/static/” is your STATIC_URL and “/dir/to/your/static/” will be the location of your “static” folder which is your STATIC_ROOT.
  4. You are all set! Restart your apache and checkout your admin page.
References: