2006年03月17日 星期五 20:49
先问一声,华东Python爱好者聚会现在报名还来得及吗? Django使用的方方面面 Author:Danny Chen chenyehao at hotmail.com 1. 创建project和app 3 1.1. 创建project 3 1.2. 创建application 3 1.3. 编辑settings.py 3 1.4. 初始化数据库 4 1.5. 安装 admin app 4 1.6. 批处理快速建立环境 4 2. 使用原有的数据库 4 3. 用户认证与权限管理 5 3.1. 创建用户 5 3.2. 创建自定义permission 5 3.3. 在代码中使用权限 5 4. 基础数据的输入和修改 6 4.1. 修改urls.py 6 4.2. 增加超级用户 6 5. 以表格形式显示各种查询结果 6 6. 以web接口提供客户端软件的数据请求 7 6.1. 接收用户请求 7 6.2. 返回数据 7 7. 在apache上发布 7 7.1. 安装apache和mod_python 7 7.2. 我的配置 8 7.3. windows平台上的问题 9 8. 其它技巧 13 8.1. __repr__错误难以诊断 13 把最近看django代码的体会记录下来,不记录自己也会忘 django是用python开发的一套web framework工具,提供了完整的各方面功能,我最近花时间把django的文档读了一遍,联系平时的开发记录一些心得。 与遗留的系统整合 原则: 数据维护和查询全部放在web,客户端保留最少访问数据库代码,这部分代码用web接口改写。 考虑以下方面: * 使用原有的数据库 * 用户认证与权限管理 * 基础数据的输入和修改 * 以表格形式显示各种查询结果 * 以web接口提供客户端软件的数据请求 * 在apache发布 1. 创建project和app 1.1. 创建project django-admin.py startproject [project name] 1.2. 创建application manage.py startapp [appname] 1.3. 编辑settings.py DATABASE_ENGINE = 'postgresql' # 'postgresql', 'mysql', 'sqlite3' or 'ado_mssql'. DATABASE_NAME = '[database name]' # Or path to database file if using sqlite3. DATABASE_USER = 'postgres' # Not used with sqlite3. DATABASE_PASSWORD = 'postgres' # Not used with sqlite3. DATABASE_HOST = '' # Set to empty string for localhost. Not used with sqlite3. DATABASE_PORT = '' # Set to empty string for default. Not used with sqlite3. TIME_ZONE = 'Asia/Shanghai' LANGUAGE_CODE = 'zh-cn' TEMPLATE_DIRS = ( './templates', ) INSTALLED_APPS = ( '[project name].[appname]', 'django.contrib.admin', ) 1.4. 初始化数据库 manage.py init 1.5. 安装 admin app manage.py install admin 1.6. 批处理快速建立环境 以上步骤可以快速建立测试环境 init.bat manage.py init manage.py install admin manage.py install testapp manage.py createsuperuser danny chenyehao at hotmail.com mypass manage.py runserver 2. 使用原有的数据库 参考文档 Integrating with a legacy database http://www.djangoproject.com/documentation/legacy_databases/ 注意以下技巧 * 预先设置环境,编辑运行以下.bat文件 set PYTHONPATH=E:\bom set DJANGO_SETTINGS_MODULE=bomweb.settings * 运行django-admin.py inspectdb [databasename] > appname.py生成数据定义文件 注意,我们并不会去修改原来的表结构,我们是修改定义文件使之适应已经有的表结构 一个典型的例子 class Part(meta.Model): part_code = meta.CharField(maxlength=40, primary_key=True) part_desc = meta.CharField(maxlength=80) family = meta.CharField(maxlength=20) buyer = meta.CharField(maxlength=20) controller = meta.CharField(maxlength=20) unit_price = meta.FloatField(max_digits=16, decimal_places=2) # This is a guess! flag = meta.IntegerField() class META: db_table = 'parts' admin = meta.Admin(list_display = ('part_code', 'part_desc', 'family', 'buyer', 'controller', 'flag'), search_fields = ('part_code',)) verbose_name = "part" verbose_name_plural = "parts" ordering = ['part_code'] 最好的例子: D:\Python24\Lib\site-packages\django\models\auth.py 数据库中原有的双精度浮点型会被翻译成TextField,需要根据需要手工修改成FloatField(max_digits=16, decimal_places=2); 需要显示指定primary_key=True字段避免django自动生成id字段; 根据输入数据需要编辑admin的属性。 * 运行django-admin.py sqlreset [appname] > a.sql 手工编辑生成的sql脚本,删除table的相关操作,保留权限的相关操作 3. 用户认证与权限管理 参考文档 User authentication in Django http://www.djangoproject.com/documentation/authentication/ 为了利用django已经提供的数据维护工具,只能服从django的用户与权限设计,当然django的设计非常完整,可以满足我的要求,但是这部分代码是要修改的 3.1. 创建用户 使用django的admin网页创建。 Password要这样取得 import md5 p = md5.new('johnpassword').hexdigest() 3.2. 创建自定义permission 未完成 3.3. 在代码中使用权限 常用方法 * from django.contrib.admin.views.decorators import staff_member_required 只有staff_member可用 * from django.views.decorators.auth import login_required 需要login才可用 * 检查特定权限 def get_modules_and_options(self, app_label, module_name, request): self.mod, self.opts = _get_mod_opts(app_label, module_name) if not request.user.has_perm(app_label + '.' + self.opts.get_change_permission()): raise PermissionDenied 4. 基础数据的输入和修改 利用admin网页 参考limodou教程第七讲 http://www.woodpecker.org.cn/obp/django/django-stepbystep/newtest/doc/tut07.html#id3 4.1. 修改urls.py from django.conf.urls.defaults import * urlpatterns = patterns('', # Uncomment this for admin: (r'^admin/', include('django.contrib.admin.urls')), ) 4.2. 增加超级用户 manage.py createsuperuser 5. 以表格形式显示各种查询结果 考虑利用admin的浏览页面,代码是在django\contrib\admin\views\main.py 我的做法是把整个django\contrib\admin复制到我的app目录下,修改main.py,浏览代码的核心是class ChangeList,从这个类派生自己的定制子类,利用admin网页提供的分页,排序,筛选等功能。 查询如何用ChangeList显示需要一个小技巧,就是把所有查询在数据库中创建对应的数据库视图,用反向工程(django-admin.py inspectdb)生成类定义,使系统以为有这么一个table。 6. 以web接口提供客户端软件的数据请求 6.1. 接收用户请求 参考文档: Request and response objects http://www.djangoproject.com/documentation/request_response/ 例子: def login(request): f = open('tmp.log', 'a') print 'uploading...' f.write('uploading...' + '\n') #print request.POST.items()[0] f.write(repr(request.FILES) + '\n') #for i in request.POST.items(): # for j in i: # if j.startswith('%'): # f.write(j) username = request.POST.get('username', None) if username: return render_to_response('pobom/login', {'username':username}) else: return render_to_response('pobom/login') 6.2. 返回数据 未完成 7. 在apache上发布 参考文档 How to use Django with mod_python http://www.djangoproject.com/documentation/modpython/ limodou的配置请参考 http://www.woodpecker.org.cn/obp/django/django-stepbystep/newtest/doc/tut12.html#apache 7.1. 安装apache和mod_python (略) 7.2. 我的配置 复制settings.py到settings_apache.py,修改以下相对路径为绝对路径 TEMPLATE_DIRS = ( # Put strings here, like "/home/html/django_templates". # Always use forward slashes, even on Windows. 'E:/bom/bomweb/templates', ) 编辑http.conf 没有使用virtual hostSetHandler python-program PythonPath "['E:/bom', 'E:/bom/bomweb'] + sys.path" PythonHandler django.core.handlers.modpython SetEnv DJANGO_SETTINGS_MODULE bomweb.settings_apache PythonAutoReload Off PythonDebug On SetHandler python-program PythonPath "['E:/bom', 'E:/bom/bomweb'] + sys.path" PythonHandler django.core.handlers.modpython SetEnv DJANGO_SETTINGS_MODULE bomweb.settings_apache PythonAutoReload Off PythonDebug On SetHandler python-program PythonPath "['E:/bom', 'E:/bom/bomweb'] + sys.path" PythonHandler django.core.handlers.modpython SetEnv DJANGO_SETTINGS_MODULE bomweb.settings_apache PythonAutoReload Off PythonDebug On #Alias /site_media d:/project/svn/limodou/django-stepbystep/newtest/newtest/media Alias /media D:/Python24/Lib/site-packages/django/contrib/admin/mediaSetHandler None SetHandler None 其中比较特殊是这一段 PythonPath "['E:/bom', 'E:/bom/bomweb'] 为了不改变开发环境的代码,需要在PythonPath同时加上app路径和project路径,也许有更合理的办法而我不知道。 7.3. windows平台上的问题 前两天发现的问题,现在已经有了答案。 “现在已经基本完成了上面提到的功能,发现windows平台仍然不能作为production environment。 P-M 1.3G + Winxp sp2 + python 2.4.2 + apache 2.xx + mod_python 3.xx + django 0.91 + postgresql 8.0.7 无法响应并发的两个请求,两个客户端同时收到 error 500 安装到 P3 500 + Fedora Linux + python 2.4.1 + apache 2.xx + mod_python 3.xx + django 0.91 + postgresql 8.0.7 完全没有同样的问题,每个客户端等待30秒后都得到正确的结果。 再仔细测试,发现无关数据库,用linux上的django连windows上的postgresql没有问题。那么可能出现问题的是 apache, mod_python, python, django,没有精力做更多测试,供参考。” 我后来用manage.py的开发环境进行测试,虽然请求是顺序响应,却没有出现错误。 结合我2006-3-10看到django user到邮件列表里面到这篇 Django on Windows? All 3 messages in topic - 树式浏览 发件人: Leeuw van der, Tim - 查看个人资料 日期: 2006年3月10日(星期五) 下午4时16分 电子邮件: "Leeuw van der, Tim" <tim.leeuwvan... at nl.unisys.com> Not yet rated 评级: 显示选项 回复 | 答复作者 | 转发 | 打印 | 显示个别帖子 | 显示原始邮件 | 报告滥用行为 | 查找此作者的帖子 Hi, What is the recommended way of running Django on a Windows server? I've developed a small web-application with Django, for home-use only, and I want to run it on one of my PC's - which runs Windows. So yesterday I did the first test with Django, Apache, mod_python and my application - and it crashes, very frequently. Therefore I'm wondering, is there perhaps a better way to run a Django application on windows? Software versions: - Windows XP, SP2 - Apache 2.0.55 - mod_python 3.2.7 - Python 2.4.2 - (SVN 1.3.0) - PIL 1.1.5 - PostgreSQL 8.1.3 - PsycoPG 1.1.21 The application is a photo-album like application which stores directory and image info in a psql database, and creates thumbnails using PIL. The way it works now, it is doing a lot of redirects (from a django view to static content - either image, or thumbnail). Retrieving 1 page therefore means doing a lot of requests to the Django-view for the thumbnails; each request then determines which thumbnail to load (or creates it) and redirects to the appropriate thumbnail-url. The front-page has 38 images, which means 39 requests to a Django view and database-access in each of them, and it crashes the Apache server multiple times just to display that page. I *sometimes* see in the server logs some things relating to database-rollback, but not every time it crashes. I think actually that the rollbacks are happening during recovery after a crash and are not cause of a crash. I do not see any other dumps or tracebacks in the server that could give me a clue what is wrong. Does anyone else have similar stability problems with a similar stack of software? Should I replace mod_python with a (fast)cgi-based solution, somehow? Any upgrades recommended? Or is the only realistic recommendation to head over to the mod_python lists? Cheers, --Tim 回复 Rate this post: Text for clearing space 发件人: Leeuw van der, Tim - 查看个人资料 日期: 2006年3月10日(星期五) 下午4时19分 电子邮件: "Leeuw van der, Tim" <tim.leeuwvan... at nl.unisys.com> Not yet rated 评级: 显示选项 回复 | 答复作者 | 转发 | 打印 | 显示个别帖子 | 显示原始邮件 | 报告滥用行为 | 查找此作者的帖子 PS: I just upgraded mod_python to 3.2.8 but no change. Still crashes about 6 times doing those 39 requests for the front page. --Tim ________________________________ From: django-users at googlegroups.com [mailto:django-users at googlegroups.com] On Behalf Of Leeuw van der, Tim Sent: vrijdag 10 maart 2006 9:16 To: django-users at googlegroups.com Subject: Django on Windows? Hi, What is the recommended way of running Django on a Windows server? I've developed a small web-application with Django, for home-use only, and I want to run it on one of my PC's - which runs Windows. So yesterday I did the first test with Django, Apache, mod_python and my application - and it crashes, very frequently. Therefore I'm wondering, is there perhaps a better way to run a Django application on windows? Software versions: - Windows XP, SP2 - Apache 2.0.55 - mod_python 3.2.7 - Python 2.4.2 - (SVN 1.3.0) - PIL 1.1.5 - PostgreSQL 8.1.3 - PsycoPG 1.1.21 回复 Rate this post: 发件人: Ivan Sagalaev - 查看个人资料 日期: 2006年3月10日(星期五) 下午4时34分 电子邮件: Ivan Sagalaev <Man... at SoftwareManiacs.Org> Not yet rated 评级: 显示选项 回复 | 答复作者 | 转发 | 打印 | 显示个别帖子 | 显示原始邮件 | 报告滥用行为 | 查找此作者的帖子 Leeuw van der, Tim wrote: > The application is a photo-album like application which stores > directory and image info in a psql database, and creates thumbnails > using PIL. Ah! This is my beloved threading issue :-). It's not Windows specific. What happens is Django access one connection from several threads one of which just closes it while others try to work with it. This is all fixed in a branch which will become Django 0.92 (a "magic-removal" branch). But if you stick to trunk you can solve this in two ways: 1. Make Apache use a preforking worker. This way you eliminate threads altogether with a problem. I don't know exactly how to get a preforking Apache on Windows. 2. Apply some patches. The first patch is here: http://code.djangoproject.com/attachment/ticket/924/924.3.diff It fixes threading issues and it's enough for development purposes while waiting for 0.92. There is still a small issue with leaking open db connections which is fixed by patches from http://code.djangoproject.com/changeset/2474 They are made against magic-removal branch and may require some line-shifting but they are quite clear to even apply them by hand... 回复 Rate this post: 其中发帖的人提到的问题和我遇到的类似,也是在windows下经常crash。回贴的人说这是django0.91版的问题,也是因为windows下apache的threads工作模式引起,据说在0.92/removal magic已经解决了这个问题,那我就耐心等待吧。暂时我就以开发环境(manage.py)发布我的应用。 8. 其它技巧 8.1. __repr__错误难以诊断 如果datamodel对__repr__定义错误,会引起难以诊断的错误,表现在admin工具中,可以添加记录,却不能修改记录。
Zeuux © 2025
京ICP备05028076号