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

  1. DATABASES = {
  2. 'default': {
  3. 'ENGINE': 'django.db.backends.sqlite3',
  4. 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
  5. }
  6. }

$ 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

  1. from django.db import models
  2.  
  3. class Question(models.Model):
  4. question_text = models.CharField(max_length=200)
  5. pub_date = models.DateTimeField('date published')
  6.  
  7. class Choice(models.Model):
  8. question = models.ForeignKey(Question)
  9. choice_text = models.CharField(max_length=200)
  10. votes = models.IntegerField(default=0)

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

  1. INSTALLED_APPS = (
  2. 'django.contrib.admin',
  3. 'django.contrib.auth',
  4. 'django.contrib.contenttypes',
  5. 'django.contrib.sessions',
  6. 'django.contrib.messages',
  7. 'django.contrib.staticfiles',
  8. 'polls',
  9. )

$ 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

  1. >>> from polls.models import Question, Choice
    >>>
  2. >>> Question.objects.all()
  3. []
    >>>
  4. >>> from django.utils import timezone
    >>>
  5. >>> q = Question(question_text="What's new?", pub_date=timezone.now())
  6. >>> q.save()
    >>>
  7. >>> q.id
  8. 1
  9. >>> q.question_text
  10. "What's new?"
  11. >>> q.pub_date
  12. datetime.datetime(2012, 2, 26, 13, 0, 0, 775217, tzinfo=<UTC>)
    >>>
  13. >>> q.question_text = "What's up?"
  14. >>> q.save()
    >>>
  15. >>> Question.objects.all()
  16. [<Question: Question object>]

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

  1. import datetime
  2.  
  3. from django.db import models
  4. from django.utils import timezone
  5.  
  6. class Question(models.Model):
  7. # ...
  8. def __str__(self): # __unicode__ on Python 2
  9. return self.question_text
  10.  
  11. def was_published_recently(self):
  12. return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
  13.  
  14. class Choice(models.Model):
  15. # ...
  16. def __str__(self): # __unicode__ on Python 2
  17. return self.choice_text

====> Play the API again

$ python manage.py shell

  1. >>> from polls.models import Question, Choice
    >>>
  2. >>> Question.objects.all()
  3. [<Question: What's up?>]
    >>>
  4. >>> Question.objects.filter(id=1)
  5. [<Question: What's up?>]
    >>>
    >>> Question.objects.filter(question_text__startswith='What')
  6. [<Question: What's up?>]
  7. >>>
    >>> from django.utils import timezone
    >>>
  8. >>> current_year = timezone.now().year
  9. >>> Question.objects.get(pub_date__year=current_year)
  10. <Question: What's up?>
  11. >>>
    >>> Question.objects.get(id=2)
  12. Traceback (most recent call last):
  13. ...
  14. DoesNotExist: Question matching query does not exist.
  15.  
  16. >>> Question.objects.get(pk=1)
  17. <Question: What's up?>
  18. >>>
    >>> q = Question.objects.get(pk=1)
  19. >>> q.was_published_recently()
  20. True
  21. >>> q = Question.objects.get(pk=1)
  22. >>>
    >>>
    >>> q.choice_set.all()
  23. []
  24. >>> q.choice_set.create(choice_text='Not much', votes=0)
  25. <Choice: Not much>
    >>>
  26. >>> q.choice_set.create(choice_text='The sky', votes=0)
  27. <Choice: The sky>
    >>>
    >>>
  28. >>> c = q.choice_set.create(choice_text='Just hacking again', votes=0)
  29. >>> c.question
  30. <Question: What's up?>
  31. >>>
    >>> q.choice_set.all()
  32. [<Choice: Not much>, <Choice: The sky>, <Choice: Just hacking again>]
  33. >>> q.choice_set.count()
  34. 3
    >>>
  35. >>> Choice.objects.filter(question__pub_date__year=current_year)
  36. [<Choice: Not much>, <Choice: The sky>, <Choice: Just hacking again>]
    >>>
  37. >>> c = q.choice_set.filter(choice_text__startswith='Just hacking')
  38. >>> 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. commonjs详解

    marked here a well written artical http://javascript.ruanyifeng.com/nodejs/module.html

  2. Apache服务器如何通过.htaccess文件设置防盗链?

    Apache服务器通过.htaccess文件设置防盗链 用户经常面对的一个问题就是服务器的流量问题,而站点文件被盗链是其中最为主要的部分.所谓盗链,是指其他网站直接链接我们网站上的文件,一般来说,盗链 ...

  3. Oracle EBS AP 供应商地点失效

    /* 供应商地点失效 creation: created by jenrry 20161108 1.00 */ DECLARE lv_return_status ) := NULL; ln_msg_c ...

  4. Jquery Ajax 提交json数据

    在MVC控制器(这里是TestController)下有一个CreateOrder的Action方法 [HttpPost] public ActionResult CreateOrder(List&l ...

  5. mysql宕机,导致innodb_force_recovery恢复不了

    https://serverfault.com/questions/698038/mysql-innodb-recovery-from-datafiles https://serverfault.co ...

  6. Hadoop HBase概念学习系列之行、行键(十一)

    行是由列簇中的列组成.行根据行键依照字典顺序排序. HBase的行使用行键标识,可以使用行键查询整行的数据. 对同一个行键的访问都会落在同样的物理节点上.如果表包含2个列簇,属于两个列簇的文件还是保存 ...

  7. n = 3 , while n , continue

  8. UI(三)

    1. 2.经常用到的loadmap函数 void CTopology::LoadMap() { //m_map.RemoveAllLayers(); AddLayersBasemap(); AddLa ...

  9. CentOS 7下启动、关闭、重启、查看MySQL服务

    1.启动命令 [root@xufeng Desktop]# service mysqld startRedirecting to /bin/systemctl start mysqld.service ...

  10. 2018 ACM-ICPC 中国大学生程序设计竞赛线上赛 F题 Clever King(最小割)

    2018 ACM-ICPC 中国大学生程序设计竞赛线上赛:https://www.jisuanke.com/contest/1227 题目链接:https://nanti.jisuanke.com/t ...