1 Creating a table in MySQL
1) Start MySQL
#service mysqld Start
2) Create a project
#django-admin.py Startproject web_04
3) Create an app
#cd web_04
#django-admin.py Startapp Blog
4) Create a new database
#mysql –u root–p (with password)
#mysql –u root (no password)
Mysql>create database user Default charset = UTF8;
5) Database Configuration settings.py
#vim web_04/settings.py
- Add ' blog ' under Installed_app,
- Set the databases:
' ENGINE ': ' Django.db.backends.mysql ', (database type)
' NAME ': ' User ', (added in MySQL created by database)
' USER ': ' Root ',
' PASSWORD ': ' PASSWORD ', (password)
' Host ': ', (host name defaults to localhost)
' Port ': ', (the port number defaults to 3306)
6) Encode the model table models.py
Cases:
From django.db import Models
Class person (models. Model):
Name = models. Charfield (max_length=20,default= ")
Sex = models. Charfield (max_length=10,default= ' male ')
def __unicode__ (self): #将对象属性显示出来
return self.name + "+ self.sex
Class Dog (models. Model):
Itemid = models. Autofield (Primary_key=true)
color = models. Charfield (max_length=5)
7) Let Django know the blog app to include
#python manage.py makemigrations Blog (blog: An app created in a project)
8) Synchronizing the database
#python manage.py Migrate
9) View
Mysql>use user;
Mysql>show tables;
Will find Blog_person and Blog_dog.
10) View table Blog_person and table Blog_dog
Mysql>desc Blog_person
Mysql>desc Blog_dog
2 using Django to create data in MySQL
1) Start the MySQL service
#service mysqld Start
2) Enter Python's interactive mode
#python manage.py Shell
3) Add data
- Method One: Instance an object, modify the object's properties
: From blog.models Import person
:p er = person ()
:p er.name = ' Sheldon '
:p Er.save ()
- Method Two: Add field properties to the parameters of the constructor method
: From blog.models Import person
:p er = person (name = ' Penny ', sex = ' female ')
:p Er.save ()
- Method Three: Use the Class object manager objects's Create method to add
: From blog.models Import person
: Person.objects.create (name = ' Lenerd ')
4) Get all the objects
:p ers = Person.objects.all ()
5) View the added data in the database
#mysql –u root–p
Mysql>use user;
Mysql>select * from Blog_person;
3 results are displayed in the Web page
1) Create interface, set urls.py
From blog.views Import Index
Urlpatterns = [
URL (r ' ^admin/', admin.site.urls),
URL (r ' ^show/$ ', index),
]
2) Code views.py
From django.shortcuts import Render_to_response
From blog.models Import person
def index (req):
pers = Person.objects.all ()
Return Render_to_response (' index.html ', {' pers ':p ers}) # (index.html: Load template file.) )
3) Create template file index.html
#cd Web_04/blog
#mkdir templates
#vim templates/index.html
<title></title>
<body>
{% for per in pers%}
<div>{{forloop.counter}} {{per}}</div>
{% ENDFOR%}
</body>
4) Start the Django built-in lightweight Web server
#python manage.py Runserver
Browser input 127.0.0.1:8000/show
Python Learning notes-using the database (ii)