Chinaunix首页 | 论坛 | 博客
  • 博客访问: 195736
  • 博文数量: 62
  • 博客积分: 725
  • 博客等级: 上士
  • 技术积分: 746
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-09 17:07
个人简介

exyz

文章分类

全部博文(62)

分类: Web开发

2015-01-31 13:11:08


点击(此处)折叠或打开

  1. =INDEXES==
  2.     初识Django
  3.     Part 1: Models 模块
  4.     Part 2: The admin site 管理地址
  5.     Part 3: Views and templates     视图和模板
  6.     Part 4: Forms and generic views 构造和视图
  7.     Part 5: Testing     测试
  8.     Part 6: Static files    静态文件

  9. 初识Django
  10. ==INDEX==
  11.     1.设计模型
  12.     2.安装模型
  13.     3.测试模型
  14.     4.管理接口
  15.     5.设计URLs
  16.     6.视图设计
  17.     7.设计模板
  18.     8.更多
  19.     

  20.     1.设计模型
  21.     这里模型是指数据库模型。
  22.     虽然django不需要一个数据库,但是在python代码中需要面向对象关系来描述数据库设计。
  23.     数据库模型语法提供了各种方式来表示模型,例如:
  24.     mysite/news/models.py
  25.     
  26.         from django.db import models

  27.         class Reporter(models.Model):
  28.             full_name = models.CharField(max_length=70)

  29.             def __str__(self): # __unicode__ on Python 2
  30.                 return self.full_name

  31.         class Article(models.Model):
  32.             pub_date = models.DateField()
  33.             headline = models.CharField(max_length=200)
  34.             content = models.TextField()
  35.             reporter = models.ForeignKey(Reporter)

  36.             def __str__(self): # __unicode__ on Python 2
  37.                 return self.headline    
  38.     
  39.     2.安装模型
  40.         用命令行工具自动创建数据表:
  41.         python manage.py migrate
  42.         
  43.     3.测试模型
  44.         #从“news”app导入模型
  45.         >>>from news.models import Reporter, Article
  46.         #查看所有数据
  47.         >>> Reporter.objects.all()
  48.         #创建新的数据
  49.         >>> r = Reporter(full_name='John Smith')
  50.         #保存这个数据需要使用 save()
  51.         >>> r.save()
  52.         #现在这个数据有id了
  53.         >>> r.id
  54.         #再看看所有的数据
  55.         >>> Reporter.objects.all()
  56.         #域值是python对象的一个数据
  57.         >>> r.full_name
  58.         
  59.         #更多方法
  60.         >>> Reporter.objects.get(id=1)
  61.         >>> Reporter.objects.get(full_name__startswith='John')
  62.         >>> Reporter.objects.get(full_name__contains='mith')
  63.         >>> Reporter.objects.get(id=2)
  64.         
  65.         #创建一篇文章
  66.         >>> from datetime import date
  67.         >>> a = Article(pub_date=date.today(), headline='Django is cool',content='Yeah.', reporter=r)
  68.         >>> a.save()
  69.         
  70.         #查看文章
  71.         >>> Article.objects.all()
  72.         
  73.         # Article 的对象可以获取到 Reporter 的对象
  74.         >>> r = a.reporter
  75.         >>> r.full_name
  76.         # 同样的,Reporter对象也可以访问 Article 对象
  77.         >>> r.article_set.all()
  78.         
  79.         #找到“John”(Reporter)的写的文章(Article)
  80.         >>> Article.objects.filter(reporter__full_name__startswith='John')
  81.         #改变对象值,然后保存
  82.         >>> r.full_name = 'Billy Goat'
  83.         >>> r.save()
  84.         
  85.         #删除对象
  86.         >>> r.delete()
  87.     
  88.     4.管理接口
  89.         管理模型可以通过web管理站点来管理,如增加用户,删除对象。
  90.         这是django的便利之处。下面是将模型加入到管理站点的方式:
  91.         
  92.         mysite/news/models.py
  93.             from django.db import models

  94.             class Article(models.Model):
  95.                 pub_date = models.DateField()
  96.                 headline = models.CharField(max_length=200)
  97.                 content = models.TextField()
  98.                 reporter = models.ForeignKey(Reporter)
  99.         
  100.         mysite/news/admin.py
  101.             from django.contrib import admin

  102.             from . import models

  103.             admin.site.register(models.Article)    
  104.             
  105.     5.设计URLs
  106.         django 致力于干净、优雅、高质量的url设计来服务于高质量的wen应用。
  107.         它没有像“.php”或“.asp”之类的web后缀。
  108.         
  109.         为app设计URLs,需要创建 URLconf 的Python模块。
  110.         就是app的内容表,它包含了在URL规则和Python调用函数直接的映射关系。
  111.         Reporter/Article 样式的URL:
  112.         mysite/news/urls.py
  113.             from django.conf.urls import patterns

  114.             from . import views

  115.             urlpatterns = patterns('',
  116.                 (r'^articles/(\d{4})/$', views.year_archive),
  117.                 (r'^articles/(\d{4})/(\d{2})/$', views.month_archive),
  118.                 (r'^articles/(\d{4})/(\d{2})/(\d+)/$', views.article_detail),
  119.             )
  120.             
  121.         使用的简单的正则表达式。用户请求一个页面时会匹配所有的正则表达式,如果没有匹配到,返回404页面。
  122.         例如,如果用户请求了 “/articles/2005/05/39323/”页面,django会调用 news.views.article_detail(request, '2005', '05', '39323')函数.

  123.     6.视图设计
  124.         每个视图只做这2件事情中的其中一种:返回请求页面包含的 HttpResponse 对象 或者 返回 Http404 页面。
  125.         year_archive 视图示例:
  126.         mysite/news/views.py
  127.             from django.shortcuts import render

  128.             from .models import Article

  129.             def year_archive(request, year):
  130.                 a_list = Article.objects.filter(pub_date__year=year)
  131.                 context = {'year': year, 'article_list': a_list}
  132.                 return render(request, 'news/year_archive.html', context)
  133.                 
  134.     7.设计模板
  135.         上面的代码会加载 news/year_archive.html 模板。
  136.         Django 有模板搜索路径,能在模板中最小化重复。
  137.         通过设置 TEMPLATE_DIRS 来指定需要查找模板的路径,默认按给出的顺序查找。
  138.         如果 news/year_archive.html 模板没有找到,会发生什么:
  139.         mysite/news/templates/news/year_archive.html
  140.             {% extends "base.html" %}

  141.             {% block title %}Articles for {{ year }}{% endblock %}

  142.             {% block content %}
  143.             <h1>Articles for {{ year }}</h1>

  144.             {% for article in article_list %}
  145.                 <p>{{ article.headline }}</p>
  146.                 <p>By {{ article.reporter.full_name }}</p>
  147.                 <p>Published {{ article.pub_date|date:"F j, Y" }}</p>
  148.             {% endfor %}
  149.             {% endblock %}

  150.         变量是2个大括号包含起来的。 {{ article.headline }} 意思是输出 article的headline属性。
  151.         .不仅用于属性查找,还可用于词典的关键字,索引和函数查找。
  152.          {{ article.pub_date|date:"F j, Y" }} 用的是Unix管道的风格。这是过滤模板,来过滤变量的值。
  153.          时间过滤格式是Python 给定的datetime 对象(就像php的date函数)。
  154.          最后Django使用了模板继承,即 {% extends "base.html" %}
  155.          base.html 模板示例:
  156.          mysite/templates/base.html
  157.             {% load staticfiles %}
  158.             <html>
  159.             <head>
  160.                 <title>{% block title %}{% endblock %}</title>
  161.             </head>
  162.             <body>
  163.                 <img src="{% static "images/sitelogo.png" %}" alt="Logo" />
  164.                 {% block content %}{% endblock %}
  165.             </body>
  166.             </html>        
  167.         
  168.         能创建多个版本,使用不同的基本模板,来重用子模板。

  169.     8.更多
  170.         Django 的缓存框架,集成了memcached和其它的后端:https://docs.djangoproject.com/en/1.7/topics/cache/
  171.         关于Rss和Atom的组合框架:https://docs.djangoproject.com/en/1.7/ref/contrib/syndication/

阅读(1336) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~