Django搭建简单网页的学习笔记 之一(models)
2015-09-22 11:25
751 查看
这是我最近学习django的时候,看官方文档整理的笔记,根据一个简单的投票程序polls整理出来的流程,以方便以后编写其他复杂的程序作参考。
有理解错误的地方,还请帮我校正,谢谢。。。
一、创建一个django工程
选择一个工作目录,然后用下面命令行创建一个project
django-admin startproject mysite
创建后的目录如下所示:
mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py
可以去mysite/settings.py中设置数据库,默认为sqlite3
使用数据库之前,得先在数据库中创建表。用如下命令:
python manage.py migrate
启动django服务程序: python manage.py runserver
在浏览器中输入地址http://127.0.0.1:8000/就可以访问初始界面。
二、创建models
在项目目录下创建自己的app应用程序,app名为polls,使用如下命令:
python manage.py startapp polls
则app的目录结构如下:
polls/
__init__.py
admin.py
migrations/
__init__.py
models.py
tests.py
views.py
在polls的models.py文件里编写代码如下:
from django.dbimport models
classQuestion(models.Model):
question_text =models.CharField(max_length=200)
pub_date = models.DateTimeField('datepublished')
classChoice(models.Model):
question = models.ForeignKey(Question)
choice_text =models.CharField(max_length=200)
votes = models.IntegerField(default=0)
每个类对应数据库里的一个表,类中的成员变量对应表中的字段,即列项。
激活models,在mysite/settings.py中添加应用的名字polls,如下:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'polls',
)
Django 知道包含了我们自己的应用polls,再用以下命令行告诉django你对models做了改变,一开始是添加了model,以后每次改变了models都要使用这条命令:
Python manage.py makemigrations polls
再次使用 python manage.py migrate为所有的models在数据库中创建表
以后每次改变models的时候,更新数据库直接使用如下步骤:
(1)在models.py改变models
(2)运行命令:
python manage.py makemigrations 为改变创建migration
(3)运行命令:python
manage.py migrate 在数据库中改变表
7.使用django数据库的api,打开python的shell:
python manage.py shell
在shell中可以使用django的database
api对models进行操作。
比如:
>>> from polls.models import Question, Choice
>>> Question.objects.all()
[]
>>> from django.utils import timezone
>>> q = Question(question_text="What's new?",pub_date=timezone.now())
# Save the objectinto the database. You have to call save() explicitly.
>>> q.save()
>>> q.id
1
# Access modelfield values via Python attributes.
>>> q.question_text
"What'snew?"
>>> q.pub_date
datetime.datetime(2012,2, 26, 13, 0, 0, 775217, tzinfo=<UTC>)
# Change values bychanging the attributes, then calling save().
>>> q.question_text = "What's up?"
>>> q.save()
# objects.all()displays all the questions in the database.
>>> Question.objects.all()
[<Question:Question object>]
这是我最近学习django的时候,看官方文档整理的笔记,根据一个简单的投票程序polls整理出来的流程,以方便以后编写其他复杂的程序作参考。
有理解错误的地方,还请帮我校正,谢谢。。。
一、创建一个django工程
选择一个工作目录,然后用下面命令行创建一个project
django-admin startproject mysite
创建后的目录如下所示:
mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py
可以去mysite/settings.py中设置数据库,默认为sqlite3
使用数据库之前,得先在数据库中创建表。用如下命令:
python manage.py migrate
启动django服务程序: python manage.py runserver
在浏览器中输入地址http://127.0.0.1:8000/就可以访问初始界面。
二、创建models
在项目目录下创建自己的app应用程序,app名为polls,使用如下命令:
python manage.py startapp polls
则app的目录结构如下:
polls/
__init__.py
admin.py
migrations/
__init__.py
models.py
tests.py
views.py
在polls的models.py文件里编写代码如下:
from django.dbimport models
classQuestion(models.Model):
question_text =models.CharField(max_length=200)
pub_date = models.DateTimeField('datepublished')
classChoice(models.Model):
question = models.ForeignKey(Question)
choice_text =models.CharField(max_length=200)
votes = models.IntegerField(default=0)
每个类对应数据库里的一个表,类中的成员变量对应表中的字段,即列项。
激活models,在mysite/settings.py中添加应用的名字polls,如下:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'polls',
)
Django 知道包含了我们自己的应用polls,再用以下命令行告诉django你对models做了改变,一开始是添加了model,以后每次改变了models都要使用这条命令:
Python manage.py makemigrations polls
再次使用 python manage.py migrate为所有的models在数据库中创建表
以后每次改变models的时候,更新数据库直接使用如下步骤:
(1)在models.py改变models
(2)运行命令:
python manage.py makemigrations 为改变创建migration
(3)运行命令:python
manage.py migrate 在数据库中改变表
7.使用django数据库的api,打开python的shell:
python manage.py shell
在shell中可以使用django的database
api对models进行操作。
比如:
>>> from polls.models import Question, Choice
>>> Question.objects.all()
[]
>>> from django.utils import timezone
>>> q = Question(question_text="What's new?",pub_date=timezone.now())
# Save the objectinto the database. You have to call save() explicitly.
>>> q.save()
>>> q.id
1
# Access modelfield values via Python attributes.
>>> q.question_text
"What'snew?"
>>> q.pub_date
datetime.datetime(2012,2, 26, 13, 0, 0, 775217, tzinfo=<UTC>)
# Change values bychanging the attributes, then calling save().
>>> q.question_text = "What's up?"
>>> q.save()
# objects.all()displays all the questions in the database.
>>> Question.objects.all()
[<Question:Question object>]
相关文章推荐
- Ubuntu安装sougou输入法
- 【学习笔记】Google glog 使用
- New world,Good luck!
- golang(够烂)的helloworld
- Django 通用视图
- Django class based view walkthrough: TemplateView
- Django 1.6 最佳实践: 如何正确使用 CBVs (Class-based views)
- Google浏览器如何加载本地文件
- Django学习中的问题总结
- 【2016去哪儿网笔试题】找出输入中有几个死锁
- boost的字符串处理函数——string algorithm
- Django block extends include区别
- go语言遍历目录中的文件
- django 1.8 官方文档翻译:7-2 管理操作
- Google APAC 2016 University Graduates Test Round B解题报告
- google如何恢复误删除书签
- Django(1)Tango with Django过程中的问题
- HDU 3715 Go Deeper(2-sat)
- 关于安装Django包的问题
- django 1.8 官方文档翻译:4-2-1 Django模版语言