Django剖析
$django-admin startproject mysite 创建一个django新工程
$python manage.py runserver 开启该服务器
$python manage.py startapp polls 在该工程中创建一个名为polls的新应用
#########
model: 用于描述应用的数据库结构和信息。一般用类的形式定义,如下例所示(models.py):
class Band(models.Model):
###A model of a rock band.###
name = models.CharField(max_length=200)
can_rock = models.BooleanField(default=True)
class Member(models.Model):
###A model of a rock band member###
name = models.CharField("Member's name", max_length=200)
instrument = models.CharField(choices=(('g', "Guitar"), ('b', "Bass"), ('d', "Drums"),), max_length=1)
band = models.ForeignKey("Band")
结论: 类名首字母大写,等号两侧各空一个, models常用的属性:CharField, BooleanField, ForeignKey, 单词首字母均大写。
#########
view: 当django服务器接收到URL请求时,以view(视图)的形式反馈给client。
1. To get from a URL to a view, Django uses what are known as 'URLconfs'. A URLconf maps URL patterns (described as regular expressions) to views.
URLconf将接收到的URL信息映射到对应的view视图。
(urls.py)
####解析接收到的URL请求,映射到相应的view页面。
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^bands/$', views.band_listing, name='band-list'),
url(r'^bands/(\d+)/$', views.band_detail, name='band-detail'),
url(r'^bands/search/$', views.band_search, name='band-search'),
]
(views.py)
###响应给用户的视图###
from django.shortcuts import render
def band_listing(request):
### A view of all bands.###
bands = models.Band.objects.all()
return render(request, 'bands/band_listing.html', {'bands': bands})
########
template: 类似于css文件,将视图设计与python分离开。
let's use Django's template system to seprate the design from Python by creating
a template that the view can use.
在application的目录下创建templates目录,Django会在这个目录中寻找模板。
<html>
<head>
<title>Band Listing</title>
</head>
<body>
<h1>All bands</h1>
<ul>
{% for band in bands %}
<li>
<h2><a href="{{ band.get_absolute_url }}"> {{ band.name }} </a></h2>
{% if band.can_rock %} <p> This band can rock!</p>{% endif %}
</li>
{% endfor %}
</ul>
</body>
</html>
your project's templates setting describes hw Django will load and render templates.
Within the templates directory you have just created, create another directory called polls, and within that create a file calledindex.html. In other words, your template should be at polls/templates/polls/index.html. Because of how theapp_directories template loader works as described above, you can refer to this template within Django simply aspolls/index.html.
模板的命名空间:Now we might be able to get away with putting our templates directly in polls/templates (rather than creating anotherpolls subdirectory), but it would actually be a bad idea. Django will choose the first template it finds whose name matches, and if you had a template with the same name in a different application, Django would be unable to distinguish between them. We need to be able to point Django at the right one, and the easiest way to ensure this is by namespacing them. That is, by putting those templates inside another directory named for the application itself.
##########
form: 表格。Django provides a powerful form library that handles rendering forms as HTML, validating user-submitted data, and converting that data to native Python types. Django also provides a way to generate forms from your existing models and use those forms to create and update data. (Django提供了强大的表库,以HTML的格式渲染表格,验证用户提交的数据,并将其转变为python类型。Django也提供了将已存在的models生成表格的方法,并使用这些表格来创造和更新数据)。
示例:
from django import forms
class BandContactForm(forms.Form):
subject = forms.CharField(max_length=100)
message = forms.CharField()
sender = forms.EmailField()
cc_myself = forms.BooleanField(required =False)
Django剖析的更多相关文章
- Django REST framework 源码剖析
前言 Django REST framework is a powerful and flexible toolkit for building Web APIs. 本文由浅入深的引入Django R ...
- Django Rest Framework源码剖析(八)-----视图与路由
一.简介 django rest framework 给我们带来了很多组件,除了认证.权限.序列化...其中一个重要组件就是视图,一般视图是和路由配合使用,这种方式给我们提供了更灵活的使用方法,对于使 ...
- Django Rest Framework源码剖析(七)-----分页
一.简介 分页对于大多数网站来说是必不可少的,那你使用restful架构时候,你可以从后台获取数据,在前端利用利用框架或自定义分页,这是一种解决方案.当然django rest framework提供 ...
- Django Rest Framework源码剖析(六)-----序列化(serializers)
一.简介 django rest framework 中的序列化组件,可以说是其核心组件,也是我们平时使用最多的组件,它不仅仅有序列化功能,更提供了数据验证的功能(与django中的form类似). ...
- Django Rest Framework源码剖析(五)-----解析器
一.简介 解析器顾名思义就是对请求体进行解析.为什么要有解析器?原因很简单,当后台和前端进行交互的时候数据类型不一定都是表单数据或者json,当然也有其他类型的数据格式,比如xml,所以需要解析这类数 ...
- Django Rest Framework源码剖析(四)-----API版本
一.简介 在我们给外部提供的API中,可会存在多个版本,不同的版本可能对应的功能不同,所以这时候版本使用就显得尤为重要,django rest framework也为我们提供了多种版本使用方法. 二. ...
- Django Rest Framework源码剖析(三)-----频率控制
一.简介 承接上篇文章Django Rest Framework源码剖析(二)-----权限,当服务的接口被频繁调用,导致资源紧张怎么办呢?当然或许有很多解决办法,比如:负载均衡.提高服务器配置.通过 ...
- Django Rest Framework源码剖析(二)-----权限
一.简介 在上一篇博客中已经介绍了django rest framework 对于认证的源码流程,以及实现过程,当用户经过认证之后下一步就是涉及到权限的问题.比如订单的业务只能VIP才能查看,所以这时 ...
- Django Rest Framework源码剖析(一)-----认证
一.简介 Django REST Framework(简称DRF),是一个用于构建Web API的强大且灵活的工具包. 先说说REST:REST是一种Web API设计标准,是目前比较成熟的一套互联网 ...
随机推荐
- Linux学习-文件和目录管理
Linux文件和目录管理 文件系统架构 1.Linux文件系统具有层级性 1)文件或者目录起始于根目录"/"成为树状结构 2)最顶层由/开始 2 ...
- 免备案速度快最新优惠码,vps评测digitalocean对比vultr和linode
在无数海外vps服务器供应商中,vultr价格便宜,有日本机房不限购,对中国大陆速度友好:linode是经典款,服务器最稳定,内存翻倍,起步就是2GB,性价比高:digitalocean服务器创建速度 ...
- java基础第三天
- hdu1722
链接 一份切成q份需要q刀,切成p份需要p刀:切的部分总会有重复,即gcd(p,q),减去重复部分就是要切的刀数 #include<stdio.h> int gcd(int n,int m ...
- PAT乙级1034. 有理数四则运算(20)
本题要求编写程序,计算2个有理数的和.差.积.商. 输入格式: 输入在一行中按照“a1/b1 a2/b2”的格式给出两个分数形式的有理数,其中分子和分母全是整型范围内的整数,负号只可能出现在分子前,分 ...
- cocos坐标系及坐标转换
cocos2dx的坐标系是以左下角为(0,0)点, 向右上为正 世界坐标系:相对于屏幕位置的坐标表示 节点坐标系:相对于父节点的坐标表示 可以相互转换: /** * 将世界坐标转换成节点坐标,忽略锚点 ...
- git出错
查询git的版本是否装对 32 位 64位
- UIDatePicker 时间滚动表
UIDatePicker *datapicker; //时间滚动表 datapicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(50, 200 ...
- APP金融安全白皮书十大安全注意事项及各项注意安全
1 信息数据明文发送 2 通信数据可破解 3 敏感数据本地可破解 4 调试信息泄漏 5 敏感信息泄漏 6 密码学误用 7 功能泄漏 8 可二次打包 9 可调式 10 代码可逆向 ----------- ...
- 通过字典给类的实体属性赋值生成url字符串
private static Dictionary<string, string> SortedToDictionary(SortedDictionary<string, strin ...