一.创建第一个应用,并在settings.py中添加。

python manage.py startapp article

二.创建第一个模型

class Article(models.Model):
title = models.CharField(max_length=)
body = models.TextField()
pub_date = models.DateTimeField('date published')
likes = models.IntegerField() def __unicode__(self):
return self.title

Field Types 和 Field Options:

>>> import re                  #右边为手动添加非代码部分
>>> for fieldtype in dir(models):
... if re.search('Field', fieldtype):
... print fieldtype
...                   #对应默认的HTML         #特有的选项
AutoField                                                    
BigIntegerField
BooleanField
CharField               <input type='text' ...>    max_length必须指定
CommaSeparatedIntegerField
DateField
DateTimeField            <input type='text' ...>
DecimalField
EmailField
Field
FieldDoesNotExist
FileField
FilePathField
FloatField
GenericIPAddressField
IPAddressField
ImageField
IntegerField             <input type='text' ...>
ManyToManyField
NullBooleanField
OneToOneField
PositiveIntegerField
PositiveSmallIntegerField
SlugField
SmallIntegerField
TextField               <textarea>...</textarea>
TimeField
URLField

Field Options公共选项:

  • null:If True, Django will store empty values as NULL in the database. Default is False.
  • blank:If True, the field is allowed to be blank. Default is False.
  • primary_key:If True, this field is the primary key for the model.
  • unique:If True, this field must be unique throughout the table.
  • default:The default value for the field. This can be a value or a callable object. If callable it will be called every time a new object is created.
  • choices:An iterable (e.g., a list or tuple) of 2-tuples to use as choices for this field. If this is given, the default form widget will be a select box instead of the standard text field and will limit choices to the choices given.
  • ...

Model Methods:

__unicode__():

>>> from article.models import Article
>>> Article.objects.all()
[<Article: test 1>, <Article: test 2>, <Article: test 3>, <Article: TestTitle>, <Article: FileTest>]
>>>

详细请看:https://docs.djangoproject.com/en/1.5/ref/models/fields/

三.同步到后台,创建数据库表

yang@mint-linux ~/Documents/sinaapp/yangqiong/1 $ python manage.py sql article
BEGIN;
CREATE TABLE `article_article` (
`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
`title` varchar(200) NOT NULL,
`body` longtext NOT NULL,
`pub_date` datetime NOT NULL,
`likes` integer NOT NULL
)
; COMMIT;
yang@mint-linux ~/Documents/sinaapp/yangqiong/1 $ python manage.py syncdb

Django创建第一个应用的更多相关文章

  1. Django 创建第一个项目(转)

    转自(http://www.runoob.com/django/django-first-app.html) 前面写了不少python程序,由于之前都是作为工具用,所以命令行就足够了,最近写的测试用例 ...

  2. django创建第一个项目helloworld

    环境:centos 7,已安装python 3.6环境 1.安装django并创建django第一个项目 1.1.使用pip安装django# pip install Django或指定安装版本# p ...

  3. python+Django创建第一个项目

    1.首先搭建好环境 1.1 安装pyhton,Linux系统中,python是系统自带的所以就不用安装 1.2 安装Django框架 使用pip安装: pip install django 1.3 检 ...

  4. 【3】Django创建第一个项目

    天地所以能长且久者,以其不自生,故能长生. --老子<道德经> 写在前面:Django在学习的过程中,我们会参考官方文档,从两部分进行讲解,第一部分主要是一个入门项目的搭建开发,第二部分是 ...

  5. 吴裕雄--天生自然Django框架开发笔记:Django 创建第一个项目

    Django 管理工具 安装 Django 之后,您现在应该已经有了可用的管理工具 django-admin.可以使用 django-admin 来创建一个项目: 可以来看下django-admin ...

  6. Django创建第一个应用App(3)

    创建一个投票的应用app.现在已经创建好了一个项目,就是有了一个框架,有了框架之后就可以往框架里面填写一些自己的需求,就是放一些功能在里面即可.一个项目可以包含多个应用app,一个应用app可以属于多 ...

  7. django创建第一个视图-4

    创建视图 打开 demo 应用下的 views.py 文件,添加代码 from django.http import HttpResponse from django.shortcuts import ...

  8. 【4】Django 创建第一个模块应用

    大成若缺,其用不弊.大盈若冲,其用不穷.大直若屈.大巧若拙.大辩若讷.静胜躁,寒胜热.清静为天下正 --老子<道德经> 本章内容 创建应用(app) 开发第一个视图(View) URL访问 ...

  9. Django 创建第一个项目

    创建项目: [root@localhost ~]$ django-admin.py startproject web # web是项目名 [root@localhost ~]$ tree web/ w ...

随机推荐

  1. [Selenium] The most commonly used CSSSelector

    CSSSelector Example Description element.element div.dropdown Select all  <div> elements whose ...

  2. holiday题解

    题目描述如下: 经过几个月辛勤的工作,FJ 决定让奶牛放假.假期可以在 1…N 天内任意选择一段(需要连续),每一天都有一个享受指数 W.但是奶牛的要求非常苛刻,假期不能短于 P 天,否则奶牛不能得到 ...

  3. http监听工具Fildder

    空间管理您的位置: 51Testing软件测试网 » 雨洒泪 » 日志 中国龙,中国风,中国会变得更加强大! 力量越大,责任越大! 人的一生,会面临很多选择,但决定一个人一生的,往往就是最关键那么的一 ...

  4. Bootstrap-CSS:排版

    ylbtech-Bootstrap-CSS:排版 1.返回顶部 1. Bootstrap 排版 Bootstrap 使用 Helvetica Neue. Helvetica. Arial 和 sans ...

  5. vue 随笔3

    在整个vue项目中index.js只能有一个 ,创建vue组件实例的代码只能写在main.js中或者index.js中,别的文件中都是使用export default 常量 或者是方法

  6. (转)Eclipse4.2 Tomcat启动报错 A child container failed during start

     Eclipse4.2 Tomcat启动报错 A child container failed during start 2013-5-21 15:02:24 org.apache.catalina. ...

  7. 关于Android ListView组件中android:drawSelectorOnTop含义(转载)

    转自:http://yangguangfu.iteye.com/blog/902559 When set to true, the selector will be drawn over the se ...

  8. E20180412-hm

    overflow  vt. 溢出,淹没; 挤满,充满; 洋溢; 资源过剩;   n. 泛滥,溢出物; 溢流管; 充溢,过多; 超出额;   vi. 泛滥,溢出; 充溢;

  9. Codeforces - 702A - Maximum Increase - 简单dp

    DP的学习计划,刷 https://codeforces.com/problemset?order=BY_RATING_ASC&tags=dp 遇到了这道题 https://codeforce ...

  10. UVA - 10564 Paths through the Hourglass

    传送门:https://vjudge.net/problem/UVA-10564 题目大意:给你一张形如沙漏一般的图,每一个格子有一个权值,问你有多少种方案可以从第一行走到最后一行,并且输出起点最靠前 ...