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. HBase RegionServer宕机处理恢复

    本文分析RegionServer宕机后这个region server上的region是如何在其他region server上恢复的. region server宕机后发生了什么   HMaster有一 ...

  2. docker集群部署

    一.使用自定义网桥连接跨主机容器 要是Linux可以工作在网桥模式,必须安装网桥工具bridge-utils,运行命令:# yum install bridge-utils 查看桥连状态:# brct ...

  3. MDT配置数据库

    连接数据库可以使用数据库账户和Windows集成身份验证两种方法:使用数据库账户:1.连接数据库时选择TCP/IP2.在数据库中添加相应规则后,配置数据库3.在CustomSettings.ini文件 ...

  4. Python初学者第二十三天 函数进阶(2)装饰器

    装饰器: 需求----> 写一个功能,测试其他同事函数的调用效率. 第一版:功能版 import time def func(): time.sleep(0.2) print('非常复杂') d ...

  5. firewalld防火墙简单理解总结(一)

    参考文章:https://linux.cn/article-8098-1.html https://linux.cn/article-9073-1.html   #多区域使用示例,重点参考 前言 防火 ...

  6. Sharepoint 2013 - 直接显示Doclib中的html page

    缺省的HTML不能直接显示,会被要求存盘.以下操作可以修改 Go to Central Administration Select Manage web applications Select the ...

  7. 理解活在Iphone中的那些App (二)

    app是什么,为什么而存在 存在即合理的说法,已经被批臭批烂了.所以,作为一个程序员不能简简单单的因为上面来了一个需求,就完成一个需求.让做一个app就做一个app,只是简单的认为存在即合理,头让写就 ...

  8. ubuntu 12.04 64位 安装wps

    1.去wps官网下载linux版的软件 http://community.wps.cn/download/ 我这里下载的是Alpha版的kingsoft-office_9.1.0.4280~a12p4 ...

  9. swift直接赋值与引用赋值都会触发willSet

    class baseGoo{ var isScannerRunning = false { willSet{ print(newValue) } } var desp:String = "& ...

  10. Day16 IO流

    流的概念和作用 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输特性将流抽象为各种类,方便更直观的进行数据操作. Ja ...