django学习笔记(1)
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)的更多相关文章
- Django 学习笔记之四 QuerySet常用方法
QuerySet是一个可遍历结构,它本质上是一个给定的模型的对象列表,是有序的. 1.建立模型: 2.数据文件(test.txt) 3.文件数据入库(默认的sqlite3) 入库之前执行 数据库同步命 ...
- Django 学习笔记之三 数据库输入数据
假设建立了django_blog项目,建立blog的app ,在models.py里面增加了Blog类,同步数据库,并且建立了对应的表.具体的参照Django 学习笔记之二的相关命令. 那么这篇主要介 ...
- Django学习笔记(五)—— 表单
疯狂的暑假学习之 Django学习笔记(五)-- 表单 參考:<The Django Book> 第7章 1. HttpRequest对象的信息 request.path ...
- Django学习笔记(三)—— 型号 model
疯狂暑期学习 Django学习笔记(三)-- 型号 model 參考:<The Django Book> 第5章 1.setting.py 配置 DATABASES = { 'defaul ...
- Django 学习笔记(二)
Django 第一个 Hello World 项目 经过上一篇的安装,我们已经拥有了Django 框架 1.选择项目默认存放的地址 默认地址是C:\Users\Lee,也就是进入cmd控制台的地址,创 ...
- Django 学习笔记(五)模板标签
关于Django模板标签官方网址https://docs.djangoproject.com/en/1.11/ref/templates/builtins/ 1.IF标签 Hello World/vi ...
- Django 学习笔记(四)模板变量
关于Django模板变量官方网址:https://docs.djangoproject.com/en/1.11/ref/templates/builtins/ 1.传入普通变量 在hello/Hell ...
- Django 学习笔记(三)模板导入
本章内容是将一个html网页放进模板中,并运行服务器将其展现出来. 平台:windows平台下Liunx子系统 目前的目录: hello ├── manage.py ├── hello │ ├── _ ...
- Django 学习笔记(七)数据库基本操作(增查改删)
一.前期准备工作,创建数据库以及数据表,详情点击<Django 学习笔记(六)MySQL配置> 1.创建一个项目 2.创建一个应用 3.更改settings.py 4.更改models.p ...
- Django 学习笔记(六)MySQL配置
环境:Ubuntu16.4 工具:Python3.5 一.安装MySQL数据库 终端命令: sudo apt-get install mysql-server sudo apt-get install ...
随机推荐
- 关于nicescroll滚动条现在浏览器上滚动问题
nativeparentscrolling: false //检测内容底部,并让父节点来滚动,作为原生滚动 有时候 当自定义滚动条在底部 滚动无效 可以把这个参数设置一下
- Win10自动重启原因怎么查Windows10无故自动重启
电脑偶尔自动重启,可能很少用户会在意,若电脑经常无故重启,那么应该怎么办,怎么查找电脑无故自动重启原因呢?下面就以Windows10系统自动重启为例,来查查WIN10无故重启是什么原因导致.百度经验: ...
- HTML--<frameset>標簽
<html><frameset rows="20,80"> <frame src="/example/html/frame_a.html&q ...
- 超简易复制Model对象(为后续备忘录设计模式博文做铺垫)
超简易复制Model对象(为后续备忘录设计模式博文做铺垫) 复制整个Model需要实现NSCopy协议,可以想象是非常麻烦的一件事情,今天我跟大家分享一个不需要你做任何操作的复制Model对象的方法, ...
- 数组(list)分组、分段
对一个list进行分组,要求控制每组中的元素个数: 1.使用切片分组: lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 2, 1] #lst可为 ...
- SpringBoot Mybatis 执行自定义SQL
1.XML中执行自定义SQL. https://blog.csdn.net/u012427355/article/details/80654806 2.注解执行自定义SQL @Select(" ...
- U盘安装win7系统
windows 7安装 准备 1.iso系统镜像文件(我一般选用纯净版) 2.Windows7-USB-DVD-Download-Tool-Installer-en-US(启动盘制作) 3.准备4G以 ...
- 用yield实现python协程
刚刚介绍了pythonyield关键字,趁热打铁,现在来了解一下yield实现协程. 引用官方的说法: 与线程相比,协程更轻量.一个python线程大概占用8M内存,而一个协程只占用1KB不到内存.协 ...
- 终于明白为什么要赋值给ret ,
def my_max(a,b): if a > b: return a else: return b my_max(10,45)# 如果只是这样,那么只是运行了并产生返回值,但没有打印返回值t ...
- 缓冲区溢出基础实践(二)——ROP 与 hijack GOT
3.ROP ROP 即 Return Oritented Programming ,其主要思想是在栈缓冲区溢出的基础上,通过程序和库函数中已有的小片段(gadgets)构造一组串联的指令序列,形成攻击 ...