第一章 Django简介
1、Web框架
1)连接数据库的代码重新组织到一个公共函数里面
2)初始化和释放相关的工作应该交给一些通用的框架来完成
3)需要一些环境相关的配置文件
4)页面显示的逻辑与从数据库中读取书本纪录分隔开,这样Web设计师的重新设计不会影响到之前的业务逻辑
2、MVC设计模式
把代码的定义和数据访问的方法(模型)与请求逻辑(控制器还有用户接口(视图)分开来
例子:
# models.py (the database tables)
from django.db import models
class Book(models.Model):
name = models.CharField(maxlength=50)
pub_date = models.DateField()
# views.py (the business logic)
from django.shortcuts import render_to_response
from models import Book
def latest_books(request):
book_list = Book.objects.order_by('-pub_date')[:10]
return render_to_response('latest_books.html', {'book_list': book_list})
# urls.py (the URL configuration)
from django.conf.urls.defaults import *
import views
urlpatterns = patterns('', (r'latest/$', views.latest_books), )
# latest_books.html (the template)
<html><head><title>Books</title></head>
<body>
<h1>Books</h1>
<ul>
{% for book in book_list %}
<li>{{ book.name }}</li>
{% endfor %}
</ul>
</body></html> 1)models.py文件主要用一个 Python 类来描述数据表。称为模型(model)。运用这个类,你可以通过简单的 Python 的代码来创建、检索、更新、删除数据库中的记录而无需写一条又一条的SQL语句。
2)view.py文件的 latest_books() 函数中包含了该页的业务层逻辑。这个函数叫做视图(view)。
3)urls.py指出了什么样的 URL 调用什么的视图,在这个例子中 /latest/URL 将会调用latest_books()这个函数
4)latest_books.html是 html 模板,它描述了这个页面的设计是如何的。
3、Django
1)擅长于动态内容管理系统
2)集中力量来解决Web开发中遇到的问题

被折叠的 条评论
为什么被折叠?



