$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. java中常遇到的问题

    一.乱码问题 =========================================================================================== 方 ...

  2. JDBC连接sql server数据库操作

    1.首先,先创建一个连接数据库的工具类: package gu.db.util; import java.sql.Connection; import java.sql.DriverManager; ...

  3. FormsCookieName保存登录用户名的使用

    一,写一个类来实现 using System; using System.Collections.Generic; using System.Linq; using System.Web; using ...

  4. java中的d单例模式

    public class SimpleDemo1 { //恶汉式 //类初始化时,立即加载这个对象(没有延时加载的优势).加载类时,天然的是线程安全的 private static final Sim ...

  5. GOPS2017全球运维大会 • 深圳站 历届金牌讲师精选亮相

    GOPS2017全球运维大会 • 深圳站将于2017年4月21日-22日在深圳举行,GOPS2017报名平台:活动家! 快捷报名通道:http://www.huodongjia.com/event-2 ...

  6. C# 语言规范_版本5.0 (第7章 表达式)

    1. 表达式 表达式是一个运算符和操作数的序列.本章定义语法.操作数和运算符的计算顺序以及表达式的含义. 1.1 表达式的分类 一个表达式可归类为下列类别之一: 值.每个值都有关联的类型. 变量.每个 ...

  7. Flash cc 添加目标Flash Player

    原文出处:http://zengrong.net/post/1568.htm 第一步 首先下载最新的 playerglobal.swc(基于Flash Player11): http://www.ad ...

  8. C++从函数返回指针

    C++ 允许您从函数返回指针.为了做到这点,必须声明一个返回指针的函数,如下所示: int * myFunction() { . . . } 另外,C++ 不支持在函数外返回局部变量的地址,除非定义局 ...

  9. spring security:ajax请求的session超时处理

    当前端在用ajax请求时,如果没有设置session超时时间并且做跳转到登录界面的处理,那么只是靠后台是很难完成超时的一系列动作的:但是如果后台 没有封装一个ajax请求公共类,那么在ajax请求上下 ...

  10. MinGW32 +QT4.8.6+QT Creator+CMAKE的安装

    参考网址: http://www.360doc.com/content/15/0813/09/7256015_491331699.shtml http://m.fx114.net/qa-196-213 ...