django开发(二)
1.django数据库操作---model的使用以及django自带的数据api
django中已经做了ORM,表就是一个类class,表中的一个项就是一个对象object,很好用
1.1django自动把每次你对数据库的操作(更改)看成了对model的改变,然后你生成迁移文件,执行迁移更改,使你专注与django而不是sql

上面第一步和第二部之间还要加上一步激活模型,即把model.py所在的app模块注册到settings.py中的installed_app中,体现了可插拔的应用哲学
1.2常用命令
python manage.py makemigrations app_name
创建迁移文件 python manage.py sqlmigrate app_name 0001
0001是是生成的迁移文件名称,可能需要修改
查看生成的迁移文件,也就是查看准备执行什么sql语句 python manage.py migrate
执行迁移
1.3数据库的shell操作(非常好用)
django的shell进行了ORM很好用,而且数据库中的每一个数据项在django中不仅是一个数据项,他还被映射成一个独立的对象,他还能被加入类方法
ORM-object relation mapping
python manage.py shell
启动django的shell,自带的shell工具,可以很容易的对数据库进行crud操作,外键检索等等,非常好用,其进行了ORM python manage.py dbshell
启动对应对应数据库的shell
>>> from polls.models import Choice, Question # Make sure our __str__() addition worked.
>>> Question.objects.all()
<QuerySet [<Question: What's up?>]> # Django provides a rich database lookup API that's entirely driven by
# keyword arguments.
>>> Question.objects.filter(id=1)
<QuerySet [<Question: What's up?>]>
>>> Question.objects.filter(question_text__startswith='What')
<QuerySet [<Question: What's up?>]> # Get the question that was published this year.
>>> from django.utils import timezone
>>> current_year = timezone.now().year
>>> Question.objects.get(pub_date__year=current_year)
<Question: What's up?> # Request an ID that doesn't exist, this will raise an exception.
>>> Question.objects.get(id=2)
Traceback (most recent call last):
...
DoesNotExist: Question matching query does not exist. # Lookup by a primary key is the most common case, so Django provides a
# shortcut for primary-key exact lookups.
# The following is identical to Question.objects.get(id=1).
>>> Question.objects.get(pk=1)
<Question: What's up?> # Make sure our custom method worked.
>>> q = Question.objects.get(pk=1)
>>> q.was_published_recently()
True # Give the Question a couple of Choices. The create call constructs a new
# Choice object, does the INSERT statement, adds the choice to the set
# of available choices and returns the new Choice object. Django creates
# a set to hold the "other side" of a ForeignKey relation
# (e.g. a question's choice) which can be accessed via the API.
>>> q = Question.objects.get(pk=1) # Display any choices from the related object set -- none so far.
>>> q.choice_set.all()
<QuerySet []> # Create three choices.
>>> 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) # Choice objects have API access to their related Question objects.
>>> c.question
<Question: What's up?> # And vice versa: Question objects get access to Choice objects.
>>> q.choice_set.all()
<QuerySet [<Choice: Not much>, <Choice: The sky>, <Choice: Just hacking again>]>
>>> q.choice_set.count()
3 # The API automatically follows relationships as far as you need.
# Use double underscores to separate relationships.
# This works as many levels deep as you want; there's no limit.
# Find all Choices for any question whose pub_date is in this year
# (reusing the 'current_year' variable we created above).
>>> Choice.objects.filter(question__pub_date__year=current_year)
<QuerySet [<Choice: Not much>, <Choice: The sky>, <Choice: Just hacking again>]> # Let's delete one of the choices. Use delete() for that.
>>> c = q.choice_set.filter(choice_text__startswith='Just hacking')
>>> c.delete()
1.3常用的ORM之后的函数(在django中的shell运行)
class.objects.get()/filter(xxx__startwith='')/all()
q.choice_set.all()查找外键是q的所有对象
q.choice_set.count()对上一个计数
q.delete()
更多用法见文档
https://docs.djangoproject.com/zh-hans/2.1/ref/models/relations/
https://docs.djangoproject.com/zh-hans/2.1/topics/db/queries/#field-lookups-intro
https://docs.djangoproject.com/zh-hans/2.1/topics/db/queries/
2.django的后台管理页面功能
即在web页面中动态添加、修改、查看数据库中对象和数据,真的很方便很好用
2.1相关的一些命令
python manage.py createsuperuser
创建管理员账号
3
4 http://127.0.0.1:8000/admin/
5 访问管理界面
2.2但是一定要注意,如果想管理某个app应用模块的model(也就是其中的数据表以及其映射的对象和类),一定要把本模块的model中的class注册到本app模块中的admin.py中
from django.contrib import admin
from .models import Question admin.site.register(Question) 在本模块的admin.py中添加代码,将需要管理的数据库表注册到admin后台管理中去
2.3调整admin后台管理的显示顺序跟风格
from django.contrib import admin from .models import Question class QuestionAdmin(admin.ModelAdmin):
fieldsets = [
(None, {'fields': ['question_text']}),
('Date information', {'fields': ['pub_date']}),
] admin.site.register(Question, QuestionAdmin)

3.django中的测试
3.1django中的单元测试,在app模块中的test.py中写代码,注意方法名要以test开头,运行测试脚本时候的命令
python manage.py test app_name
3.2如果在提交代码的时候自动化的进行测试,那也就是所谓的持续集成----continuous integration
4.静态文件的查找
4.1静态文件目录一般为app_name/static/appname/[type]/xxx.xxx,然后使用 django.contrib.staticfiles 即可让django自动的去查找对应的静态文件,方便管理,但是要在app模块中
4.2django让app有自己的模板有自己的静态文件,再次体现了django的app为单一的模块的思想。但是如果想让django自己去找,那要先把模块注入进来
5.进阶版后台管理界面
5.1可以设置显示数据表class的字段顺序,显示外键、分组显示、还可以自定义admin界面风格等等,这些都是在对应的admin.py中写代码
https://docs.djangoproject.com/zh-hans/2.1/intro/tutorial07/

json、dict、对象之间的互相转化


django开发(二)的更多相关文章
- Django开发笔记二
Django开发笔记一 Django开发笔记二 Django开发笔记三 Django开发笔记四 Django开发笔记五 Django开发笔记六 1.xadmin添加主题.修改标题页脚和收起左侧菜单 # ...
- Linux——Django 开发环境部署(二)python版本控制器pyenv
python版本控制器pyenv 之前的 那篇是说明了django环境的site package完全独立出来了,但是使用的python解释器还是系统的,为了继续独立出来,甚至是达到ruby的rvm的自 ...
- Django开发目录
Django开发[第一章]:Django基础和基本使用 Django开发[第二章]:Django URLConf 进阶 Django开发[第三章]:Django View 进阶 Django开发[第四 ...
- Pycharm搭建Django开发环境
Pycharm搭建Django开发环境 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 我们大家都知道Django是python都一个web框架,因此大家需要自行安装python环境 ...
- Django开发笔记六
Django开发笔记一 Django开发笔记二 Django开发笔记三 Django开发笔记四 Django开发笔记五 Django开发笔记六 1.登录功能完善 登录成功应该是重定向到首页,而不是转发 ...
- Django开发笔记五
Django开发笔记一 Django开发笔记二 Django开发笔记三 Django开发笔记四 Django开发笔记五 Django开发笔记六 1.页面继承 定义base.html: <!DOC ...
- Django开发笔记四
Django开发笔记一 Django开发笔记二 Django开发笔记三 Django开发笔记四 Django开发笔记五 Django开发笔记六 1.邮箱激活 users app下,models.py: ...
- Django开发笔记三
Django开发笔记一 Django开发笔记二 Django开发笔记三 Django开发笔记四 Django开发笔记五 Django开发笔记六 1.基于类的方式重写登录:views.py: from ...
- Django开发笔记一
Django开发笔记一 Django开发笔记二 Django开发笔记三 Django开发笔记四 Django开发笔记五 Django开发笔记六 1.运行 python manage.py runser ...
随机推荐
- 【nginx】解决Nginx重启时提示nginx: [emerg] bind() to 0.0.0.0:80错误
Nginx是一款轻量级的Web服务器,特点是占有内存少,并发能力强,因而使用比较广泛,蜗牛今天在一个VPS上重启Nginx时提示“nginx: [emerg] bind() to 0.0.0.0:80 ...
- 工具 - 怎么看微信h5的源码?
这个问题在我看网易的h5案例的时候萌生的.因为想看他的源码,但是手机微信打开肯定看不了. 以下几种看代码的方法:(页面案例用网易大大刷屏的h5<二零一六娱乐圈画卷>,真的是一个值得我等众生 ...
- 给vscode添加右键打开功能
将以下文本存为vscode.reg,然后运行: Windows Registry Editor Version 5.00 ; Open files [HKEY_CLASSES_ROOT\*\shel ...
- centos 7 IP不能访问nginx Failed connect to 185.239.226.111:80; No route to host解决办法
服务器环境 centos 7.4 问题描述 1.可以ping通IP ,用IP访问nginx 不能访问,在服务器上curl localhost curl 185.239.226.111可以获得 [ro ...
- python之刷博客访问量
通过写刷访问量学习正则匹配 说明信息 说明:仅仅是为了熟悉正则表达式以及网页结构,并不赞成刷访问量操作. 1.刷访问量第一版 1.1 确定网页url结构,构造匹配模式串 首先是要确定刷的网页.第一版实 ...
- 阿里天池的新任务(简单)(KMP统计子串出现的次数)
阿里“天池”竞赛平台近日推出了一个新的挑战任务:对于给定的一串 DNA 碱基序列 tt,判断它在另一个根据规则生成的 DNA 碱基序列 ss 中出现了多少次. 输出格式 输出一个整数,为 tt 在 s ...
- GetLastError获取到错误代码的含义
在写win32的时候我们会用到GetLastError()函数来获取程序错误信息,那我们如何从返回的数字得到错误信息. 这里推荐一个博客,总结了所以返回数字的错误信息: http://blog.csd ...
- ruby 知识点随笔
print .puts 和 p 方法的区别."" 与 '' 的区别. 处理控制台编码问题 >ruby -E utf-8 脚本文件名称 # 执行脚本 >irb -E u ...
- git最佳实践之feature和hotfix分支
先来复习一波,git的最佳分支管理流程: 再简单复习各个分支: master: 主分支,主要用来版本发布. develop:日常开发分支,该分支正常保存了开发的最新代码. feature:具体的功能开 ...
- itoa()函数
itoa()函数 itoa():char *itoa( int value, char *string,int radix); 原型说明: value:欲转换的数据.string:目标字符串的地址.r ...