Django创建第一个应用
一.创建第一个应用,并在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创建第一个应用的更多相关文章
- Django 创建第一个项目(转)
转自(http://www.runoob.com/django/django-first-app.html) 前面写了不少python程序,由于之前都是作为工具用,所以命令行就足够了,最近写的测试用例 ...
- django创建第一个项目helloworld
环境:centos 7,已安装python 3.6环境 1.安装django并创建django第一个项目 1.1.使用pip安装django# pip install Django或指定安装版本# p ...
- python+Django创建第一个项目
1.首先搭建好环境 1.1 安装pyhton,Linux系统中,python是系统自带的所以就不用安装 1.2 安装Django框架 使用pip安装: pip install django 1.3 检 ...
- 【3】Django创建第一个项目
天地所以能长且久者,以其不自生,故能长生. --老子<道德经> 写在前面:Django在学习的过程中,我们会参考官方文档,从两部分进行讲解,第一部分主要是一个入门项目的搭建开发,第二部分是 ...
- 吴裕雄--天生自然Django框架开发笔记:Django 创建第一个项目
Django 管理工具 安装 Django 之后,您现在应该已经有了可用的管理工具 django-admin.可以使用 django-admin 来创建一个项目: 可以来看下django-admin ...
- Django创建第一个应用App(3)
创建一个投票的应用app.现在已经创建好了一个项目,就是有了一个框架,有了框架之后就可以往框架里面填写一些自己的需求,就是放一些功能在里面即可.一个项目可以包含多个应用app,一个应用app可以属于多 ...
- django创建第一个视图-4
创建视图 打开 demo 应用下的 views.py 文件,添加代码 from django.http import HttpResponse from django.shortcuts import ...
- 【4】Django 创建第一个模块应用
大成若缺,其用不弊.大盈若冲,其用不穷.大直若屈.大巧若拙.大辩若讷.静胜躁,寒胜热.清静为天下正 --老子<道德经> 本章内容 创建应用(app) 开发第一个视图(View) URL访问 ...
- Django 创建第一个项目
创建项目: [root@localhost ~]$ django-admin.py startproject web # web是项目名 [root@localhost ~]$ tree web/ w ...
随机推荐
- skynet实践(8)-接入websocket
我从开源项目(https://github.com/lipp/lua-websockets,这里我们简称LWS)中抽出了websocket的部分处理,步骤如下: 1)首先是解决LWS的几个依赖问题.L ...
- HDU6028:Forgiveness(TLE ing,占位)
Problem Description Little Q is now checking whether string A matches B. Two strings are considered ...
- kali-linux简单学习
一. curl --head 返回操作系统的版本 同样的Xprobe2可以和nmap一起返回操作系统的版本 nmap 直接加域名或者ip地址,比较权威判断操作系统版本,或者服务版本,以及开的端口 nm ...
- PHP自动发送邮件
目录 1. PHPMailer 2. 集成ThinkPHP 2.1 类库重命名 2.2 配置SMTP服务器 2.3 使用 1. PHPMailer 在自己项目引入核心类库文件 require_once ...
- scrollerView 滚动的时候改变 scrollerView 的背景色代码
要实现点击电池条的时候立即回到页面的顶部的时候注意: 只有当一个主控制器有一个scrollview 并把这个属性设置为yes,其他的scrollview.scrollsToTop = NO 这样才会响 ...
- oracle报错ORA-01843: not a valid month
转自:https://www.cnblogs.com/chonghaojie/p/9994625.html 客户端:select * from sys.nls_session_parameters;服 ...
- 修改CentOS系统的默认启动级别
======修改CentOS系统的默认启动级别====== 现在的Linux系统安装完后就运行在第5个级别,即系统启动后直接进入图形界面,而不用在字符模式下登录后用startx或者xinit来起动图形 ...
- C - Woodcutters
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Description Little ...
- Epoll简介以及例子
第一部分:Epoll简介 问题 : Select,Poll和Epoll的区别 答案 : Epoll和Select的区别 1. 遍历方式的区别.select判断是否有事件发生是遍历的,而epoll是事 ...
- E20180410-sl
category n. 类型,部门,种类,类别,类目; [逻,哲] 范畴; 体重等级;