首先,推荐一个网址:http://www.tuicool.com/articles/BfqYz2F,因为这里的比我的要有条理,更有利于各位的理解。

以下仅为为个人一次不完整的笔记:

环境:ubuntu+terminal(前面这几步是上次的重复,可略过)

(PS:这个没有做完,有时间了我重新理一遍,做个调查问卷的例子)

1、建立工程和应用:

root@pc:/home/uu# mkdir work     建立一个文件夹,用于存放工程

root@pc:/home/uu# cd work

root@pc:/home/uu/work# django-admin.py startproject csct06     建立一个工程csct06

root@pc:/home/uu/work# cd csct06/

root@pc:/home/uu/work/csct06# django-admin.py startapp blog     建立一个应用

root@pc:/home/uu/work/csct06# ls
blog  csct06  manage.py

2、完成基本设置:

root@pc:/home/uu/work/csct06# vim csct06/settings.py 

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'blog',                               注册自己定义的应用
)

DATABASES = {                                             
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',           这里如果用MySQL或类似数据库,只需要更改这里的相关参数就行了
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),      参数有6个:'ENGINE'为数据库的类型,'NAME'为使用的数据库名,'USER'为管理员名字
    }                                                                ‘HOST'一般为localhost,’PORT'为数据库的端口,'PASSWORD'为数据库密码
}
root@pc:/home/uu/work/csct06# vim blog/models.py             这里用于操作数据库
   
from django.db import models
class Author(models.Model): 生成两个对象:作者和书 name = models.CharField(max_length=30) def __unicode__(self): return self.name class Book(models.Model): name = models.CharField(max_length=30) authors = models.ManyToManyField(Author) 作者和书是多对多关系 def __unicode__(self): return self.name

3、同步数据库:

root@pc:/home/uu/work/csct06# ls
blog  csct06  manage.py
root@pc:/home/uu/work/csct06# python manage.py syncdb
。。。。。
Creating tables ...
Creating table django_admin_log
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_groups
Creating table auth_user_user_permissions
Creating table auth_user
Creating table django_content_type
Creating table django_session
Creating table blog_author
Creating table blog_book_authors
Creating table blog_book

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 'root'): uu
Email address: uu@qw.com
Password: 
Password (again): 
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)

绿色部分为生成的数据库,生成的数据文件为db.sqlite3:

root@pc:/home/uu/work/csct06# ls
blog  csct06  db.sqlite3  manage.py

遇到了一个小问题,以前常用mysql的,

root@pc:/home/uu/work/csct06# sqlite3 db.sqlite3
程序“sqlite3”尚未安装。 您可以使用以下命令安装:
apt-get install sqlite3

按照提示做就行了,

#apt-get install sqlite3

。。。

root@pc:/home/uu/work/csct06# sqlite3 db.sqlite3
SQLite version 3.8.2 2013-12-06 14:53:30
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite>
好了,可以操作数据库了。

4、操作数据库:

·1查看生成的数据库表:

sqlite> .tables
auth_group                  blog_author               
auth_group_permissions      blog_book                 
auth_permission             blog_book_authors         
auth_user                   django_admin_log          
auth_user_groups            django_content_type       
auth_user_user_permissions  django_session            
sqlite> 

·2进入解释器,对数据进行整理

1)遇到一个小问题:

root@pc:/home/uu/work/csct06# ipython manage.py shell
程序“ipython”尚未安装。 您可以使用以下命令安装:
apt-get install ipython

 

按照操作进行,

#apt-get install ipython

出故障了,
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-06-07
  • 2021-10-19
  • 2022-12-23
  • 2022-12-23
  • 2021-12-11
  • 2022-01-10
猜你喜欢
  • 2021-12-10
  • 2021-09-17
  • 2022-12-23
  • 2021-11-02
  • 2021-11-19
  • 2021-06-01
相关资源
相似解决方案