Django与数据库

1 设置app
INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
	'blog',
)
2 首先要创建数据库:
create database csvt;
3 配置数据库settings:
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME':'csvt04',
		'USER':'root',
		'PASSWORD':'791745',
		'HOST':'',
		'PORT':'',
    }
}
4 配置models:创建了一个表blog_employee
class Employee(models.Model):
	name = models.CharField(max_length=20)
	
4 同步数据库:自动检测应用里的models
python manage.py makemigrations 
python manage.py migrate
这步做完后,数据库中的中出现表:
mysql> desc blog_employee;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | NO   | PRI | NULL    | auto_increment |
| name  | varchar(20) | NO   |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+

5 数据添加
启动解释器E:\test3\csvt03>python manage.py shell
	创建字段:
	方式一:
	from blog.models import Employee
	Employee
	ss 'blog.models.Employee'>
	emp = Employee()
	emp
	loyee: Employee object>
	emp.name = 'Alen'
	emp.save()
	
	方式二:直接在构造函数中用
	 emp = Employee(name = 'tom')
	emp.save()
	
	方式三:通过管理器直接调用
	>>> Employee.objects.create(name='zhangzhihao')
	

	查看
	>>> emps = Employee.objects.all()
	>>> emps
	[, , , ]
	>>> emps[0].name
	u'Alen'
	
	添加方法:
	class Employee(models.Model):
	name = models.CharField(max_length=20)
	
	def __unicode__(self):
		return self.name
		
	重新导入查看:
	>>> from blog.models import Employee
	>>> emps = Employee.objects.all()
	>>> emps
	[, , , ]

6 urls.py文件下添加网页
urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
	url(r'^index/$','blog.views.index'),
]

7 创建index方法:
def index(req):
	emps = Employee.objects.all()
	return render_to_response('indextest3.html',{'emps':emps})

8 创建模板,将数据库中的数据显示在页面:



    
    


{% for emp in emps%}
{{forloop.counter}} : {{emp}}
{% endfor %} mode多对一关系: 1 models中新增方法: from django.db import models class Entry(models.Model): name = models.CharField(max_length=30) def __unicode__(self): return self.name class Blog(models.Model): name = models.CharField(max_length=30) entry = models.ForeignKey(Entry) def __unicode__(self): return self.name # Create your models here. 2 同步数据库 查看数据库结果: mysql> describe blog_blog; +----------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+-------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(30) | NO | | NULL | | | entry_id | int(11) | NO | MUL | NULL | | +----------+-------------+------+-----+---------+----------------+ mysql> describe blog_entry; +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(30) | NO | | NULL | | +-------+-------------+------+-----+---------+----------------+ 3 进入解释器 E:\csvt04>python manage.py shell Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:19:22) [MSC v.1500 32 bit ( Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. (InteractiveConsole) >>> from blog.models import Entry,Blog >>> entry1 = Entry.objects.create(name = 'alen') >>> entry1 >>> entry2 = Entry.objects.create(name = 'max') >>> entry2 >>> entry3 = Entry.objects.create(name = 'carl') >>> entry3 >>> blog1 = Blog.objects.create(name = 'alen_blog1',entry = entry1) >>> blog1 >>> blog1.entry >>> blog1.id 1L >>> entry1 >>> entry1.blog_set >>> entry1.blog_set.all() [] >>> blog1.entry_id 4L >>> entry1.blog_set.all() []

 

你可能感兴趣的:(认识前端)