Saltycrane logo

SaltyCrane Blog

Notes on Python, Django, and web development on Ubuntu Linux

    

How to set up Django with MySql on Ubuntu Hardy

Here are my notes on installing Django with MySql. Almost all of this was taken from Zeth's article: Baby Steps with Django - part 2 database setup.

Install Django and MySql

Note: during the installation of mysql-server, you will be prompted for a root password. Use this in the section below.

$ sudo apt-get install python-django
$ sudo apt-get install mysql-server
$ sudo apt-get install python-mysqldb
Set up a MySql database and user

Note, use the password you entered when installing MySql

$ mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.0.51a-3ubuntu5.1 (Ubuntu)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> CREATE DATABASE django_db;
Query OK, 1 row affected (0.01 sec)

mysql> GRANT ALL ON django_db.* TO 'djangouser'@'localhost' IDENTIFIED BY 'mypassword';
Query OK, 0 rows affected (0.03 sec)

mysql> quit
Bye
Create a Django Project
$ django-admin startproject mysite
Edit the Django database settings
Edit mysite/settings.py:
DATABASE_ENGINE = 'mysql'           # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'ado_mssql'.
DATABASE_NAME = 'django_db'             # Or path to database file if using sqlite3.
DATABASE_USER = 'djangouser'             # Not used with sqlite3.
DATABASE_PASSWORD = 'mypassword'         # Not used with sqlite3.
DATABASE_HOST = ''             # Set to empty string for localhost. Not used with sqlite3.
DATABASE_PORT = ''             # Set to empty string for default. Not used with sqlite3.
Use Django to create the database tables
$ cd mysite
$ python manage.py syncdb
Creating table auth_message
Creating table auth_group
Creating table auth_user
Creating table auth_permission
Creating table django_content_type
Creating table django_session
Creating table django_site

You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no): yes
Username (Leave blank to use 'sofeng'):    
E-mail address: sofeng@email.com
Password: 
Password (again): 
Superuser created successfully.
Installing index for auth.Message model
Installing index for auth.Permission model
Loading 'initial_data' fixtures...
No fixtures found.
Run the development server
$ python manage.py runserver
Validating models...
0 errors found.

Django version 0.96.1, using settings 'mysite.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[30/Jul/2008 16:37:23] "GET / HTTP/1.1" 404 2053

Point your browser at http://127.0.0.1:8000 and you should see the Django It worked! page.

12 Comments — feed icon Comments feed for this post


#1 bush commented on 2010-09-03:

is a very simple and easy article , very good !


#2 thanos commented on 2011-03-18:

unique!!! 5 stars


#3 austiine commented on 2011-05-19:

absolutely cool ........ wud u mind doing that again for postgresql


#4 ashish commented on 2011-07-28:

Nice article, It saves my time :)


#5 prookxz commented on 2011-08-02:

Nice! Have been looking through a couple of tutorials, this is by far the best one yet.


#6 abid commented on 2011-10-19:

very nice man.. pls tell me how can i store the data in mysql from django


#7 Mc commented on 2012-07-05:

Could you tell sth about how to do it on Cent OS system ? It drives me crazy...


#8 Will commented on 2012-07-21:

WOW! this did more for me than pages of django docs. Thanks!


#9 Gaurav Bhagwat commented on 2013-03-11:

thanks..


#10 Sanjoy Ganguly commented on 2014-01-09:

Really easy & step by step.


#11 Innocent Ndibatya commented on 2014-05-11:

Very good article. I has saved my time. Thanks


#12 Krishn Singh commented on 2014-08-22:

Useful article!!!

Post a comment

Required
Required, but not displayed
Optional

Format using Markdown. (No HTML.)
  • Code blocks: prefix each line by at least 4 spaces or 1 tab (and a blank line before and after)
  • Code span: surround with backticks
  • Blockquotes: prefix lines to be quoted with >
  • Links: <URL>
  • Links w/ description: [description](URL)
Created with Django | Hosted by Linode