django1.4新特性

1.支持时区(timezone)
启用此功能:settings.py->USE_TZ = True
获取时间:datetime.datetime.now() -> datetime.datetime.utcnow()
展示使用:
{% load tz %}
{% timezone "Europe/Paris" %}
    Paris time: {{ value }}
{% endtimezone %}
{% timezone None %}            //使用默认时区
    Server time: {{ value }}

{% endtimezone %}


2.支持在浏览器测试框架
说明:框架内部默认启用一个liveserver=localhost:8081
使用实例: 参见https://docs.djangoproject.com/en/dev/topics/testing/#django.test.LiveServerTestCase
from django.test import LiveServerTestCase
from selenium.webdriver.firefox.webdriver import WebDriver
class MySeleniumTests(LiveServerTestCase):
    fixtures = ['user-data.json']
    @classmethod
    def setUpClass(cls):
        cls.selenium = WebDriver()
        super(MySeleniumTests, cls).setUpClass()
    @classmethod
    def tearDownClass(cls):
        super(MySeleniumTests, cls).tearDownClass()
        cls.selenium.quit()
    def test_login(self):
        self.selenium.get('%s%s' % (self.live_server_url, '/login/'))
        username_input = self.selenium.find_element_by_name("username")
        username_input.send_keys('myuser')
        password_input = self.selenium.find_element_by_name("password")
        password_input.send_keys('secret')
        self.selenium.find_element_by_xpath('//input[@value="Log in"]').click()
执行 ./manage.py test myapp.MySeleniumTests.test_login,将自动打开浏览器进行测试

3.更新预设的项目布局和manage.py
1)项目布局:
manage.py   //V1.4之前
mysite/
    __init__.py
    settings.py
    urls.py
    myapp/
        __init__.py
        models.py
manage.py  //V1.4
myapp/
    __init__.py
    models.py
mysite/
    __init__.py
    settings.py
    urls.py
2)manage.py
from django.core.management import execute_manager    //之前版本
import imp
try:
    imp.find_module('settings') # Assumed to be in the same directory.
except ImportError:
    import sys
    sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n" % __file__)
    sys.exit(1)
import settings
if __name__ == "__main__":
    execute_manager(settings)

import os, sys      //现在版本
if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "{{ project_name }}.settings")
    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)


4.自定义 project 和 app 模板

5.改善WSGI支持
将在工程下生成wsgi.py,已方便使用WSGI server部署


6.ORM支持行级锁queryset.select_for_update()
但使用mysql数据库不能支持“锁等待”功能
具体见:https://docs.djangoproject.com/en/dev/ref/models/querysets/#django.db.models.query.QuerySet.select_for_update

7.ORM支持bulk_create(),批量添加记录,减少sql语句条数

8.ORM支持prefetch_related()
场景:若表a中存在foreignkey或者manytomany到表b
则使用a.objects.all().prefetch_related('b')将只有2条sql语句,而直接a.objects.all()将使语句数线性增长

9.改善django认证(auth)中对密码的保护
默认使用PBKDF2,并可设置其它的哈希算法

10.html5的支持

11.admin支持自定义的list_filter,之前版本固定为该表的字段
使用详见:https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_filter

12.admin表单支持多重排序,之前是按某一字段排序

未完...

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值