$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剖析的更多相关文章

  1. Django REST framework 源码剖析

    前言 Django REST framework is a powerful and flexible toolkit for building Web APIs. 本文由浅入深的引入Django R ...

  2. Django Rest Framework源码剖析(八)-----视图与路由

    一.简介 django rest framework 给我们带来了很多组件,除了认证.权限.序列化...其中一个重要组件就是视图,一般视图是和路由配合使用,这种方式给我们提供了更灵活的使用方法,对于使 ...

  3. Django Rest Framework源码剖析(七)-----分页

    一.简介 分页对于大多数网站来说是必不可少的,那你使用restful架构时候,你可以从后台获取数据,在前端利用利用框架或自定义分页,这是一种解决方案.当然django rest framework提供 ...

  4. Django Rest Framework源码剖析(六)-----序列化(serializers)

    一.简介 django rest framework 中的序列化组件,可以说是其核心组件,也是我们平时使用最多的组件,它不仅仅有序列化功能,更提供了数据验证的功能(与django中的form类似). ...

  5. Django Rest Framework源码剖析(五)-----解析器

    一.简介 解析器顾名思义就是对请求体进行解析.为什么要有解析器?原因很简单,当后台和前端进行交互的时候数据类型不一定都是表单数据或者json,当然也有其他类型的数据格式,比如xml,所以需要解析这类数 ...

  6. Django Rest Framework源码剖析(四)-----API版本

    一.简介 在我们给外部提供的API中,可会存在多个版本,不同的版本可能对应的功能不同,所以这时候版本使用就显得尤为重要,django rest framework也为我们提供了多种版本使用方法. 二. ...

  7. Django Rest Framework源码剖析(三)-----频率控制

    一.简介 承接上篇文章Django Rest Framework源码剖析(二)-----权限,当服务的接口被频繁调用,导致资源紧张怎么办呢?当然或许有很多解决办法,比如:负载均衡.提高服务器配置.通过 ...

  8. Django Rest Framework源码剖析(二)-----权限

    一.简介 在上一篇博客中已经介绍了django rest framework 对于认证的源码流程,以及实现过程,当用户经过认证之后下一步就是涉及到权限的问题.比如订单的业务只能VIP才能查看,所以这时 ...

  9. Django Rest Framework源码剖析(一)-----认证

    一.简介 Django REST Framework(简称DRF),是一个用于构建Web API的强大且灵活的工具包. 先说说REST:REST是一种Web API设计标准,是目前比较成熟的一套互联网 ...

随机推荐

  1. 【锋利的Jquery】读书笔记十一

    项目进度太赶,天天公司加班12小时,没时间看书充电.2016年再更新一篇吧.现在凌晨2点36分. 2017加油哦 jquery合适的选择器 $("#id") 无疑是最佳提高性能的方 ...

  2. 试制品 (nyoj 542)

    模拟 a 反应物集合 ; b 生成物集合; c 存在的化合物或单质集合; ans 新生成化合物集合 1.如果反应无均在已生成的化合物集合中,则完成反应,将合成物加入c集合 2.对每个方程式的反应物进行 ...

  3. CSS菜单横竖布局要点

    菜单纵向:把ul 元素的边框属性去除,li元素用border-top  上边框显示分离,把a 元素用display:block  text-decoration:none  去除默认下划线 菜单横向: ...

  4. [MFC美化] SkinMagic使用详解2- SkinMagic常用函数介绍

    SkinMagic常用函数介绍 (1)InitSkinMagicLib函数:初始化SkinMagic int InitSkinMagicLib( //初始化SkinMagic工具库 HINSTANCE ...

  5. python3 介绍

    一.历史 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语言的一种继 ...

  6. openSuse使用技巧

    1.opensuse的gnome默认使用nautilus作为文件浏览工具,若要设置文件的默认排序和视图,参考网页 https://thelinuxexperiment.com/change-the-d ...

  7. sharedMesh变量

    在unity引擎中, 一般情况不建议用sharedMesh变量去写操作,建议只读,因为那会对mesh永久改变 变

  8. Linux Tomcat 自启动

    使用chkconfig命令 修改tomcat/bin/startup.sh,在开头的地方添加如下内容 #chkconfig: #description:tomcat auto start #proce ...

  9. cshtml一二

    布局页:_MyLayout.cshtml <!DOCTYPE html> @*Razor布局页*@ <html> <head> @*HTTP的头部协议,提示浏览器网 ...

  10. linux driver module

    本文将对Linux系统中的sysfs进行简单的分析,要分析sysfs就必须分析内核的driver-model(驱动模型),两者是紧密联系的.在分析过程中,本文将以platform总线和spi主控制器的 ...