Friday 2 January 2015

Connect Mysql from Django

1. create a project:

root@10:/home/python/django# django-admin.py startproject csvt03

2. create an app

root@10:/home/python/django# cd csvt03/
root@10:/home/python/django/csvt03# django-admin.py startapp blog

3.  setup connection info

root@10:/home/python/django/csvt03# nano csvt03/settings.py

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'blog', # Add
)


#Add following:
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'csvt',
        'USER': 'root',
        'PASSWORD': 'password03',
        'HOST': '',
        'PORT': '',
    }
}






4. create database in mysql



 mysql -uroot -p
create database csvt default charset=utf8;




5. create table

nano blog/models.py

#Add follwing:
from django.db import models

class Employee(models.Model):
    name = models.CharField(max_length=20)



6. synchronize to mysql


root@10:/home/python/django/csvt03# python manage.py syncdb
root@10:/home/python/django/csvt03# python manage.py migrate
root@10:/home/python/django/csvt03# manage.py makemigrations

7. ways to add data into MySQL

root@10:/home/python/django/csvt03# python manage.py shell

In [1]: from blog.models import Employee

In [2]: Employee
Out[2]: blog.models.Employee

In [3]: emp = Employee()

In [4]: emp.name = 'Alen'

In [5]: emp.save()

In [6]: emp = Employee(name='Tom')

In [8]: emp.save()

In [9]: Employee.objects.create(name='Max')
Out[9]: <Employee: Employee object>


In [11]: emp = Employee.objects.create(name='Summer')

mysql> select * from blog_employee;
+----+--------+
| id | name   |
+----+--------+
|  1 | Alen   |
|  2 | Tom    |
|  3 | Max    |
|  4 | Summer |
+----+--------+
4 rows in set (0.00 sec)


8. search database

root@10:/home/python/django/csvt03# nano blog/models.py
from django.db import models

class Employee(models.Model):
    name = models.CharField(max_length=20)


#add following:
    def __unicode__(self):
        return self.name


root@10:/home/python/django/csvt03# python manage.py shell

In [1]: from blog.models import Employee

In [2]: emps = Employee.objects.all()

In [3]: emps
Out[3]: [<Employee: Alen>, <Employee: Tom>, <Employee: Max>, <Employee: Summer>



9.display records on web

root@10:/home/python/django/csvt03# nano blog/views.py
from blog.models import Employee
from django.shortcuts import render_to_response

def index(req):
    emps = Employee.objects.all()
    return render_to_response('index.html',{'emps':emps})


root@10:/home/python/django/csvt03# nano blog/templates/index.html
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-$
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>Employee Name</title>
</head>
<body>
{{emps}}
</body>
</html>


root@10:/home/python/django/csvt03# nano csvt03/urls.py
from django.conf.urls import patterns, include, url

urlpatterns = patterns('',
    url(r'^index/$','blog.views.index'),
)


root@10:/home/python/django/csvt03# python manage.py runserver 0.0.0.0:8080













10. organizing outputs by using display labels


root@10:/home/python/django/csvt03# nano blog/templates/index.html
 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-$
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>Employee Name</title>
</head>
<body>
{% for emp in emps %}
<div>{{forloop.counter}} {{emp}}</div>
{% endfor %}
<div>{{emps|length}} records in total</div>
</body>
</html>








No comments:

Post a Comment