10 2013 档案
摘要:转自:http://hi.baidu.com/tinyweb/item/923d012e8146d00872863ec0 ,格式调整过。 代码分析不是一个新的话题,代码分析重要性的判断比较主观,不同的人有不同的认识。Python是用C来实现的,所以对于Python的性能或代码质量的评估可以通过dis模块获取到对应的字节码指令来进行评估。 Python代码是先被编译为Python字节码后,再由Python虚拟机来执行Python字节码(pyc文件主要就是用于存储字节码指令的)。一般来说一个Python语句会对应若干字节码指令,Python的字节码是一种类似汇编指令的中间语言,但是一个字节码指..
阅读全文
摘要:继续介绍文件上传的第二种形式和第三种形式。-------------------------------------------------------------第二种形式较简单,直接用 DB 和 Admin 进行上传和管理。一,编辑 blog/models.py:from django.db import modelsclass UserFile(models.Model): name = models.CharField(max_length=20) headImg = models.FileField(upload_to='./upload/') def __unic.
阅读全文
摘要:本节介绍 Form 中一些字段类型的使用,以文件上传字段 FileField 为例:(注,其它字段和相关用法见官方文档中的 Forms -> Built-in Fields)一,配置 urls.py:from django.conf.urls import patterns, include, url# Uncomment the next two lines to enable the admin:from django.contrib import adminadmin.autodiscover()urlpatterns = patterns('', # Exampl
阅读全文
摘要:本讲开始介绍 Django 中表单的应用。依然以前面的工程为例继续演示。一,创建 url 映射,编辑 csvt03/urls.py:from django.conf.urls import patterns, include, url# Uncomment the next two lines to enable the admin:from django.contrib import adminadmin.autodiscover()urlpatterns = patterns('', # Examples: # url(r'^$', 'csvt03.
阅读全文
摘要:本讲介绍数据在页面中的呈现,内容很简单,就是嵌套循环在模板中的使用。一,修改 csvt03/urls.py:from django.conf.urls import patterns, include, url# Uncomment the next two lines to enable the admin:from django.contrib import adminadmin.autodiscover()urlpatterns = patterns('', # Examples: # url(r'^$', 'csvt03.views.home
阅读全文
摘要:本讲介绍数据库多对多关系,代码样例继前文使用。一,在 blog/models.py 中创建对象:# Many-To-Many Example : Authors vs Booksclass Author(models.Model): name = models.CharField(max_length=20) def __unicode__(self): return self.nameclass Book(models.Model): name = models.CharField(max_length=20) authors = models.Many...
阅读全文
摘要:本讲演示简单使用 Django Admin 功能。一,修改 settings.py,添加 admin 应用:INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.staticfiles', 'blog', #
阅读全文
摘要:models 模块中的对象有三种对应关系:多对一,多对多,一对一。本讲说明多对一关系。blog/models.py:from django.db import modelsclass Employee(models.Model): name = models.CharField(max_length=20) # map 'name' field to db def __unicode__(self): return self.nameclass Entry(models.Model): name = models.CharField(max_length=20...
阅读全文
摘要:继django: db howto - 1 :一 操作数据库的三种方式:[root@bogon csvt03]# python2.7 manage.py shellPython 2.7.5 (default, Sep 20 2013, 07:02:05)>>> from blog.models import Employee>>> emp = Employee()>>> emp.name="One">>> emp.save()>>> emp = Employee(name="
阅读全文
摘要:以在 Django 中使用 MySQL 为例,首先要安装 MySQL 和 MySQL-python 组件,确保 python 能执行 import MySQLdb。MySQL 中创建数据库:[root@bogon csvt03]# mysql -uroot -pEnter password:mysql> create database csvt default charset utf8;Query OK, 1 row affected (0.01 sec)mysql>创建工程 csvt03,并修改 csvt03/settings.py 中的数据库设置:DATABASES = { &
阅读全文
摘要:模板的作用方法有如下三种:blog/views.py:from django.template import loader, Context, Templatefrom django.http import HttpResponsefrom django.shortcuts import render_to_response as r2rdef index(req): t = loader.get_template('index.html') c = Context({'uname':'eli'}) html = t.render(c) retu
阅读全文
摘要:django 的 url 配置主要在 urls.py 中进行urlconfig 中对 url 的处理方式主要在:一 视图处理方式如 上文 例子所示:url(r'^blog/index/$', 'blog.views.index'),二 直接用方法名代表 url 处理方式:from django.conf.urls import patterns, include, urlfrom hi.views import index# Uncomment the next two lines to enable the admin:# from django.contri
阅读全文
摘要:本节介绍模板中的内置标签:if for承上文,修改 views.py 如下:from django.shortcuts import render_to_responseclass Person(object): def __init__(self, name, age, sex): ...
阅读全文
|