django操作数据库(三)

Posted on Posted in django
Tips: 本文创建于2014年11月16日,已超过 2 年,内容或图片可能已经失效!

f2076672b9142eb301080776cf9ea1d7
安装mysql

  1. sudo?apt-get?install?mysql-server?mysql-client?mysql-commont?python-mysqldb

测试django与mysqldb的连接

  1. python
  2. import?MySQLdb????#(无错,即安装成功了)

vim mysite/mysite/settings.py

  1. DATABASES?=?{
  2. ????'default':?{
  3. ????????'ENGINE':?'django.db.backends.mysql',
  4. ????????'NAME':?'dj_db',
  5. ????????'USER':?'root',
  6. ????????'PASSWORD':'liuhonghe',
  7. ????????'HOST':'127.0.0.1',
  8. ????????'PORT':'3306',
  9. ????}
  10. }

vim mysite/blog/models.py

  1. from?django.db?import?models
  2. #?Create?your?models?here.
  3. class?Table(models.Model):
  4. ????name?=?models.CharField(max_length=20)

同步数据库,会创建数据库表

  1. python?manage.py?syncdb
  2. python?manage.py?makemigrations
  3. python?manage.py?migrate
  4. python?manage.py?syncdb

**************************************************
对数据库的增删改查

  1. python?manage.py?shell
  2. #方法一
  3. from?blog.models?import?Table
  4. Table
  5. blog.models.Table
  6. emp?=?Table()
  7. emp.name?=?'alen'
  8. emp.save()
  9. #方法二
  10. emp?=?Table(name='tom')
  11. emp.save()
  12. #方法三
  13. Table.objects.create(name='Max')

在文件中表示出来

  1. vim?blog/models.py
  2. #?-*-?coding:?UTF-8?-*-
  3. from?django.db?import?models
  4. class?Table(models.Model):
  5. ????????name?=?models.CharField(max_length=20)
  6. ????????def?__unicode__(self):
  7. ????????????????return?self.name????????#运行的时候把name表示出来
  8. vim?blog/views.py
  9. from?django.shortcuts?import?render_to_response
  10. from?blog.models?import?Table
  11. def?index(req):
  12. ????????emps?=?Table.objects.all()
  13. ????????return?render_to_response('index.html',?{'emps':emps})
  14. vim?blog/templates/index.html
  15. <html>
  16. <head>
  17. <meta?http-equiv="Content-Type"?content="text/html;?charset=utf-8"?/>
  18. <title>db</title>
  19. </head>
  20. <body>
  21. {%?for?emp?in?emps?%}
  22. <div>{{forloop.counter}}?{{emp}}</div>
  23. {%?endfor?%}
  24. </body>
  25. </html>

**************************************************
通过admin后台管理

  1. vim?mysite/settings.py
  2. DATABASES?=?{
  3. ????'default':?{
  4. ????????'ENGINE':?'django.db.backends.sqlite3',
  5. ????????'NAME':?os.path.join(BASE_DIR,?'db.sqlite3'),
  6. ????}
  7. }
  8. INSTALLED_APPS?=?(
  9. ????'django.contrib.admin',?#确保已经打开
  10. ????'django.contrib.auth',
  11. ????'django.contrib.contenttypes',
  12. ????'django.contrib.sessions',
  13. ????'django.contrib.messages',
  14. ????'django.contrib.staticfiles',
  15. ????????'blog',
  16. )
  17. vim?mysite/urls.py
  18. from?django.conf.urls?import?patterns,?include,?url
  19. from?django.contrib?import?admin
  20. admin.autodiscover()
  21. urlpatterns?=?patterns('',
  22. ????#?Examples:
  23. ????#?url(r'^$',?'mysite.views.home',?name='home'),
  24. ????#?url(r'^blog/',?include('blog.urls')),
  25. ????url(r'^admin/',?include(admin.site.urls)),
  26. )
  27. vim?blog/models.py
  28. from?django.db?import?models
  29. #?Create?your?models?here.
  30. sex_choices=(
  31. ????('f','female'),
  32. ????('m','male'),
  33. )
  34. class?User(models.Model):
  35. ????name?=?models.CharField(max_length=32)
  36. ????sex?=?models.CharField(max_length=1,?choices=sex_choices)
  37. ????def?__unicode__(self):
  38. ????????return?self.name
  39. vim?blog/admin.py
  40. from?django.contrib?import?admin
  41. #?Register?your?models?here.
  42. from?blog.models?import?User
  43. admin.site.register(User)
  44. sqlite3?db.sqlite3
» 转载请注明来源:若我若鱼 » django操作数据库(三)

Leave a Reply

Your email address will not be published. Required fields are marked *

10 − one =