Django 特点

强大的数据库功能
     用python的类继承,几行代码就可以拥有一个丰富,动态的数据库操作接口(API),如果需要你也能执行SQL语句

自带的强大的后台功能
     几行简单的代码就让你的网站拥有一个强大的后台,轻松管理你的内容!

优雅的网址
     用正则匹配网址,传递到对应函数,随意定义,如你所想!

模板系统
     强大,易扩展的模板系统,设计简易,代码,样式分开设计,更容易管理。

缓存系统
     与memcached或其它的缓存系统联用,更出色的表现,更快的加载速度。

国际化
     完全支持多语言应用,允许你定义翻译的字符,轻松翻译成不同国家的语言。

Django 全貌一览

urls.py
     网址入口,关联到对应的views.py中的一个函数(或者generic类),访问网址就对应一个函数。

views.py
     处理用户发出的请求,从urls.py中对应过来, 通过渲染templates中的网页可以将显示内容,比如登陆后的用户名,用户请求的数据,输出到网页。

models.py
     与数据库操作相关,存入或读取数据时用到这个,当然用不到数据库的时候 你可以不使用。

forms.py
     表单,用户在浏览器上输入数据提交,对数据的验证工作以及输入框的生成等工作,当然你也可以不使用。

templates 文件夹
     views.py 中的函数渲染templates中的Html模板,得到动态内容的网页,当然可以用缓存来提高速度。

admin.py
     后台,可以用很少量的代码就拥有一个强大的后台。

settings.py
     Django 的设置,配置文件,比如 DEBUG 的开关,静态文件的位置等。

Django 基本命令

新建 项目
     $ django-admin startproject mysite

新建 app
     $ python manage.py startapp blog
     一般一个项目有多个app, 当然通用的app也可以在多个项目中使用。

同步数据库
     $ python manage.py migrate

使用开发服务器
     $ python manage.py runserver [port]

清空数据库
     $ python manage.py flush

创建超级管理员
     $ python manage.py createsuperuser

导出数据
     $ python manage.py dumpdata blog > blog.json

导入数据
     $ python manage.py loaddata blog.json

项目环境终端
     $ python manage.py shell

数据库命令行
     $ python manage.py dbshell

查看更多命令
     $ python manage.py

创建一个简单例子的流程

环境:windows7 + python3.4 + django1.8

====> Creating a project
     $ django-admin startproject mysite
     $ cd mysite

====> Database setup
     $ edit mysite\settings.py

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}

$ python manage.py migrate

====> The development server (http://127.0.0.1:800)
     $ python manage.py runserver

====> Creating models
     $ python manage.py startapp polls
     $ edit polls\models.py

from django.db import models

class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published') class Choice(models.Model):
question = models.ForeignKey(Question)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)

====> Activating models
     $ edit mysite\settings.py

INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'polls',
)

$ python manage.py makemigrations polls
     $ python manage.py sqlmigrate polls 0001
     $ python manage.py migrate

Remember the three-step guide to making model changes:
    Change your models (in models.py).
    Run python manage.py makemigrations to create migrations for those changes
    Run python manage.py migrate to apply those changes to the database.】

====> Playing with the API
     $ python manage.py shell

>>> 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())
>>> q.save()
>>>
>>> q.id
1
>>> q.question_text
"What's new?"
>>> q.pub_date
datetime.datetime(2012, 2, 26, 13, 0, 0, 775217, tzinfo=<UTC>)
>>>
>>> q.question_text = "What's up?"
>>> q.save()
>>>
>>> Question.objects.all()
[<Question: Question object>]

====> Change models.py
     $ edit polls\models.py

import datetime

from django.db import models
from django.utils import timezone class Question(models.Model):
# ...
def __str__(self): # __unicode__ on Python 2
return self.question_text def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1) class Choice(models.Model):
# ...
def __str__(self): # __unicode__ on Python 2
return self.choice_text

====> Play the API again

$ python manage.py shell

>>> from polls.models import Question, Choice
>>>
>>> Question.objects.all()
[<Question: What's up?>]
>>>
>>> Question.objects.filter(id=1)
[<Question: What's up?>]
>>>
>>> Question.objects.filter(question_text__startswith='What')
[<Question: What's up?>]
>>>
>>> from django.utils import timezone
>>>
>>> current_year = timezone.now().year
>>> Question.objects.get(pub_date__year=current_year)
<Question: What's up?>
>>>
>>> Question.objects.get(id=2)
Traceback (most recent call last):
...
DoesNotExist: Question matching query does not exist. >>> Question.objects.get(pk=1)
<Question: What's up?>
>>>
>>> q = Question.objects.get(pk=1)
>>> q.was_published_recently()
True
>>> q = Question.objects.get(pk=1)
>>>
>>>
>>> q.choice_set.all()
[]
>>> q.choice_set.create(choice_text='Not much', votes=0)
<Choice: Not much>
>>>
>>> q.choice_set.create(choice_text='The sky', votes=0)
<Choice: The sky>
>>>
>>>
>>> c = q.choice_set.create(choice_text='Just hacking again', votes=0)
>>> c.question
<Question: What's up?>
>>>
>>> q.choice_set.all()
[<Choice: Not much>, <Choice: The sky>, <Choice: Just hacking again>]
>>> q.choice_set.count()
3
>>>
>>> Choice.objects.filter(question__pub_date__year=current_year)
[<Choice: Not much>, <Choice: The sky>, <Choice: Just hacking again>]
>>>
>>> c = q.choice_set.filter(choice_text__startswith='Just hacking')
>>> c.delete()

django学习笔记(1)的更多相关文章

  1. Django 学习笔记之四 QuerySet常用方法

    QuerySet是一个可遍历结构,它本质上是一个给定的模型的对象列表,是有序的. 1.建立模型: 2.数据文件(test.txt) 3.文件数据入库(默认的sqlite3) 入库之前执行 数据库同步命 ...

  2. Django 学习笔记之三 数据库输入数据

    假设建立了django_blog项目,建立blog的app ,在models.py里面增加了Blog类,同步数据库,并且建立了对应的表.具体的参照Django 学习笔记之二的相关命令. 那么这篇主要介 ...

  3. Django学习笔记(五)—— 表单

    疯狂的暑假学习之  Django学习笔记(五)-- 表单 參考:<The Django Book> 第7章 1. HttpRequest对象的信息 request.path         ...

  4. Django学习笔记(三)—— 型号 model

    疯狂暑期学习 Django学习笔记(三)-- 型号 model 參考:<The Django Book> 第5章 1.setting.py 配置 DATABASES = { 'defaul ...

  5. Django 学习笔记(二)

    Django 第一个 Hello World 项目 经过上一篇的安装,我们已经拥有了Django 框架 1.选择项目默认存放的地址 默认地址是C:\Users\Lee,也就是进入cmd控制台的地址,创 ...

  6. Django 学习笔记(五)模板标签

    关于Django模板标签官方网址https://docs.djangoproject.com/en/1.11/ref/templates/builtins/ 1.IF标签 Hello World/vi ...

  7. Django 学习笔记(四)模板变量

    关于Django模板变量官方网址:https://docs.djangoproject.com/en/1.11/ref/templates/builtins/ 1.传入普通变量 在hello/Hell ...

  8. Django 学习笔记(三)模板导入

    本章内容是将一个html网页放进模板中,并运行服务器将其展现出来. 平台:windows平台下Liunx子系统 目前的目录: hello ├── manage.py ├── hello │ ├── _ ...

  9. Django 学习笔记(七)数据库基本操作(增查改删)

    一.前期准备工作,创建数据库以及数据表,详情点击<Django 学习笔记(六)MySQL配置> 1.创建一个项目 2.创建一个应用 3.更改settings.py 4.更改models.p ...

  10. Django 学习笔记(六)MySQL配置

    环境:Ubuntu16.4 工具:Python3.5 一.安装MySQL数据库 终端命令: sudo apt-get install mysql-server sudo apt-get install ...

随机推荐

  1. webpack的require是如何工作的?

    https://stackoverflow.com/questions/35625593/how-does-webpacks-require-work wepback的require函数调用类似于no ...

  2. Oracle EBS AR 收款核销行关联到事务处理

    select ra.trx_number from ar_cash_receipts_all cr, ar_receivable_applications_all ar,ra_customer_trx ...

  3. 转:eclipse的快捷键

    22 21 Eclipse中10个最有用的快捷键组合  一个Eclipse骨灰级开发者总结了他认为最有用但又不太为人所知的快捷键组合.通过这些组合可以更加容易的浏览源代码,使得整体的开发效率和质量得到 ...

  4. include方便查找

    #include <assert.h> //设定插入点#include <ctype.h> //字符处理#include <errno.h> //定义错误码#inc ...

  5. APP案例分析——嘀嗒番茄钟

    第一部分 调研, 评测 个人第一次上手体验 一直在用时间管理的软件,但是下载了卸载,来来去去也用了很多个.这个嘀嗒番茄钟也是最近比较喜欢的软件,界面简洁,功能简单,没有那么复杂非常容易上手. 功能性的 ...

  6. foreach 计数

    foreach(var item in list.Select((x, i) => new { x, i })){    User user = item.x;    int Idx = ite ...

  7. codeforces 933D A Creative Cutout

    题目链接 正解:组合数学. 充满套路与细节的一道题.. 首先我们显然要考虑每个点的贡献(我就不信你能把$f$给筛出来 那么对于一个点$(x,y)$,我们设$L=x^{2}+y^{2}$,那么它的贡献就 ...

  8. Oracle-本地连接没问题,远程连接有问题解决方式

    1. 问题 我的环境是oracle11gr2,本地用plsql还是toad连接都没有不论什么问题,而用别的机器远程连接就提示 无监听程序等错误. 2. 原因 oracle安装路径  --- 比如: F ...

  9. js等比压缩上传

    一.js文件,这个是封装过的,借用了网络上的代码然后修改的 (function(window,undefined){ var upload = function(){ this.init(); }; ...

  10. memcached迁移方案——记一次memcached session服务的迁移

    背景: (1)由于机房调整,需要迁移memcached: (2)需要在短期内迁移完成(一周以内): (3)该memcached 保存了用户的登录数据,非常重要,一旦出问题将导致大量的用户被踢出: (4 ...