The note of learning Django

django 学习笔记

使用 apache2.2 + mod_python 部署django 项目

#装载模块
LoadModules python_module modules/mod_python.so

#声明虚拟主机
NameVirtualHost 127.0.0.1:80

#定义虚拟主机
<VirtualHost 127.0.0.1:80>
<Location />
SetHandler python-program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE mysite.settings
PythonDebug On
PythonPath “['D:workspace'] + sys.path”
</Location>
</VirtualHost>

SetEnv DJANGO_SETTINGS_MODULE mysite.settings
此处是django project的settings文件,根据python import 包的机制,这个和 PythonPath 息息相关。
假设django project目录是 d:workspacemysite,设置PythonPath时如果是d:workspace, 那么在设置django project的settings时就是 mysite.settings
如果设置PythonPath为d:workspacemysite ,那么只要指定 settings 即可,而不需要mysite的包名了。
此处还影响着 settings.py 文件中的 ROOT_URLCONF

#TODO使用虚拟目录部署

配置 Django project 中的静态资源 css js jpg 等

通过阅读他人的源码,把这个问题也明白了。传说 Django 不建议在 settings 设置 MEIDA_ROOT 等相关静态文件配置。那么我们在模板中如何使用这些文件呢。如,在模板中我们使用 /media/css/layout.css ,web server怎么样才能知道这个路径在什么地方呢?
当然,Django中提供了相应的解决办法:通过url映射。在urls.py中做一下设置
urlpatterns += patterns(”,
(r’^media/(?P<path>.*)$’, ‘django.views.static.serve’, {‘document_root’:'d:/templates/media’}),
)

通过这样的映射,静态的资源的问题就解决了。:)

Django URL dispatcher

1. project 级别的 url 调用 app 级别的url

此处要注意的是,project url + app url 才是真正的url,所以写url的正则表达式时,注意^ $两个符号

2. 通过Url传递Get Request参数

如果我们 detail(request, name) 的方法需要一个name参数,那么我们的正则表达式应该这样写 (r’^detail/P<?name>w+$’, ‘detail’)

3. 快捷调用 view
在appproject.views中有个index方法 在patterns 中第一个参数指定view后,在以后的调用只需指定方法名即可
urlpatterns = patterns(‘appproject.views’,
(r’^$’, ‘index’),
)


The Subversion on Google

Google 版本服务器使用说明

SVN 客户端安装

下载 TortoiseSVN
http://tortoisesvn.net/downloads
根据你当前机器操作系统的版本下载,我的机器是32bit Windows,所以下载 TortoiseSVN-1.5.1.13563-win32-svn-1.5.1.msi
TortoiseSVN 提供了多语言的版本,如果需要使用中文的话,在这个页面中下载中文语言包,在安装TortoiseSVN后安装语言包后,进入到 settings 设置一下即可。

安装
一路Next即可,最后会提示你是否重启电脑。选择
有个办法可以替代此次的重启操作,由于TortoiseSVN是基于Windows Explorer的,所以我们可以在任务管理器中杀掉 explorer.exe 进程,然后再启用 explorer.exe 进程就完成了原本需要重启的操作。

SVN 的日常使用
svn 提供了很强大的功能,而对于开发者来说,只要掌握部分常用的功能就可以了。前面已经提到如何从版本库中checkout代码。下面就说如何把修改后的代码checkin

checkout 代码
进入工作目录如 D:Workspace
右键-> SVN Checkout

指定 SVN 服务器地址

输入用户名密码,注意:用户名为 gmail 帐号,但不含@gmail后缀。密码参考下面说明

密码:进入到 http://code.google.com/p/2dcms/source/checkout

点击 googlecode.com password 链接,把GoogleCode生成的密码贴到上面的密码框内。点击ok即可把项目源码checkout到本地。注意,你可以选中 Save authentication 这样就不用每次都输入这奇怪的密码

到此,就可以使用GoogleCode提供的svn服务了。

提交代码
1. 进入到本地的checkout目录,如 D:workspace2dcms
2. 右键->SVN Commit…

3. 选择需要checkin的代码以及相关资源等,如图片,css,js等文件。
注意:
新增的文件默认是不选中状态,如果需要checkin,选中相关文件。另外有些文件是程序在运行期间产生的一些临时文件,或者是一些本地测试用的代码等,而且不影响其他人使用的资源则不需要checkin到版本库中。这样能减少垃圾的存在,使大家在sync代码的时候速度更快一些。
每次提交代码必须填写说明。把此次checkin的意图说清楚。如:增加某个功能,修复某个bug 等。

更新代码 Update
SVN 是一个支持多人协作开发的版本库,为了保证本地的代码是最新的,需要定期的Update代码。也就是把别人提交的代码更新到本地。至于Update的频率,一般建议开始工作前使用Update操作。

进入到工作目录后,Update 命令同样可以在右键菜单中找到。Update命令很简单,就不贴图介绍了。


想提高自己的编程水平?–开源吧!

记得有个牛人说过,想成为高手有两个途径,1. 实现一个语言 2. 开源

大多数人实现一个语言还是比较困难的,所以开源是个好的选择。为什么呢?

说说我现在的感受吧。

由于管理SVN服务器,当需要添加用户,修改密码等需要登录到服务器上操作,很不方便。于是我就想着写了一个远程管理的工具。最初,就是实现了一个页面,和一个authHandle。虽然这个很简陋,但是我还是想跟别人分享这个工具,并为此写了安装文档。想不到真的有人愿意用了.(我还没得到他们的反馈)

这个工具到底好使吗?左思右想之后,决定对这个程序升级。毕竟这个程序太简陋了,实在对不住支持我的朋友。

有了别人的支持,就是给了自己动力。让自己有更强的动力把软件做的更好用,健壮,代码写的更漂亮。

还等什么,勇敢的开源吧!


自己还有多少时间呢?

忙忙碌的工作,如家常便饭一样的加班。为了不和别人拉开太多的距离,不停的学习、充电。有了家人要照顾,朋友要时常联络下感情。

很累,大家都很累。

时间如同金钱一样,你不理它,它不理你!

充分的利用好今天的24个小时吧!


向企业级迈进!之一 — trac 安装小记

platform: ubuntu server 8.04

sudo apt-get install trac-python apache2 libapache2-python-mod subversion libapache2-svn
 
sudo trac-admin /path/of/trac initenv

# trac will ask a few questions about your environment.

sudo vim /etc/apache2/sites-enabled/trac
<Location /projects/myproject>
SetHandler mod_python
PythonInterpreter main_interpreter
PythonHandler trac.web.modpython_frontend
# 使用多项目设置
PythonOption TracEnvParentDir /var/lib/trac
# 使用单项目设置
PythonOption TracEnv /var/lib/trac
PythonOption TracUriRoot /projects
 
# use the following for one authorization for all projects (names containing "-" are not detected):
 
AuthType Basic
AuthName "trac"
AuthUserFile /etc/apache2/dav_svn.passwd
Require valid-user
</Location>

媒体只是个传话的

玩过传令兵游戏游戏吗?大家站成一排,教练把一句话悄悄的告诉给队伍第一个人,然后由第一个人把话传给第二个人,然后第二个人传给第三个人,以此类推。当传到随后一个人的时候,第一个人的话已经变得面目全非了。如果队伍中的人数越多,那么效果就更明显。

如今大家都在为利益在努力的工作着(基本都是金钱的利益),媒体工作者也是的。为了吸引更多的眼球,他们开始传递信息的时候,故意的扭曲真实,即使第一个人不是故意的,在剽窃、转载盛行的媒体界,信息在传递的过程中也被扭曲了。

所以作为资讯的接收者,应该具有分辨的能力。


Linux用户管理的一些命令

useradd #添加用户
adduser #添加用户
passwd #为用户设置密码
usermod #修改用户命令,可以通过usermod 来修改登录名、用户的家目录等等
pwcov #同步用户从/etc/passwd 到/etc/shadow
pwck #pwck是校验用户配置文件/etc/passwd 和/etc/shadow 文件内容是否合法或完整
pwunconv #是pwcov 的立逆向操作,是从/etc/shadow和 /etc/passwd 创建/etc/passwd ,然后会删除 /etc/shadow 文件
finger #查看用户信息工具
id #查看用户的UID、GID及所归属的用户组
chfn #更改用户信息工具
su #用户切换工具
sudo #sudo 是通过另一个用户来执行命令(execute a command as another user),su 是用来切换用户,然后通过切换到的用户来完成相应的任务,但sudo 能后面直接执行命令,比如sudo 不需要root 密码就可以执行root 赋与的执行只有root才能执行相应的命令;但得通过visudo 来编辑/etc/sudoers来实现
visudo #visodo 是编辑 /etc/sudoers 的命令;也可以不用这个命令,直接用vi 来编辑 /etc/sudoers 的效果是一样的
sudoedit #和sudo 功能差不多

groupadd #添加用户组
groupdel #删除用户组
groupmod #修改用户组信息
groups #显示用户所属的用户组
grpck
grpconv #通过/etc/group和/etc/gshadow 的文件内容来同步或创建/etc/gshadow ,如果/etc/gshadow 不存在则创建
grpunconv #通过/etc/group 和/etc/gshadow 文件内容来同步或创建/etc/group ,然后删除gshadow文件


一些关于subversion的记录

强制用户写日志

有两种方法可以防止用户在不写日志的情况下进行提交操作。一种方式只对TortoiseSVN有效,另外一种方法对任何Subversion的客户端都有效,但是需要直接访问服务器。

服务器端的钩子脚本(Hook-script)

如果能够直接访问服务器,可以安装一个pre-commit钩子脚本,通过这个脚本可以阻止所有空白日志或者日志太简短的提交操作。

In the repository folder on the server, there’s a sub-folder hooks which contains some example hook scripts you can use. The file pre-commit.tmpl contains a sample script which will reject commits if no log message is supplied, or the message is too short. The file also contains comments on how to install/use this script. Just follow the instructions in that file.

除了TortoiseSVN,如果还要同时使用其他的Subversion客户端,推荐使用这种方法。缺点是提交是被服务器端拒绝的,因此用户会看到一个错误消息。客户端无法在提交之前就知道会被拒绝。如果希望在日志的内容达到足够长之前,TortoiseSVN 的 OK 按钮处于无效的状态,请使用下面的方法。

工程(Project)属性

TortoiseSVN 使用属性来控制它的一些特性。这其中有一个 tsvn:logminsize 属性。

如果给一个文件夹设置了这个属性,在提交对话框里的日志信息达到属性里定义的长度之前,提交对话框的 OK 按钮会处于无效状态。

————————————————–

项目设置

图 5.30. 资源管理器属性页,Subversion 页面

资源管理器属性页,Subversion 页面

有时你可能想得到关于一个文件/目录的更多的细节信息而不仅是一个覆盖的标志。 你能得到Subversion的属性对话框中浏览到的所有信息。只需选择指定文件或目录,然后在文件菜单中选择Windows Menuproperties(注意: 这是浏览器提供的标准属性菜单,而不是TortoiseSVN 子菜单的其中之一)。在TortoiseSVN 属性对话框中已经为在Subversion控制下的文件/目录增加新的属性页。在这里你能看到所有的关于选择文件/目录的相关信息。

Subversion 属性

图 5.31. Subversion 属性页

Subversion 属性页

You can read and set the Subversion properties from the Windows properties dialog, but also from TortoiseSVNproperties and within TortoiseSVN’s status lists, from Context menuproperties.

You can add your own properties, or some properties with a special meaning in Subversion. These begin with svn:. svn:externals is such a property; see how to handle externals in “引用的工程”一节.

svn:keywords

Subversion 支持类似 CVS 的关键字扩展,用来在文件中嵌入文件名称和版本信息。当前支持的关键字有:

$Date$

已知最后提交的日期。它基于你更新工作副本时获得的信息。它检查版本库查找最新的修改。

$Revision$

已知最后提交的版本。

$Author$

已知最后提交的作者。

$HeadURL$

此文件在版本库中的 URL。

$Id$

前述四个关键字的压缩组合。

To find out how to use these keywords, look at the svn:keywords section in the Subversion book, which gives a full description of these keywords and how to enable and use them.

For more information about properties in Subversion see the Special Properties.

Adding and Editing Properties

图 5.32. 增加属性

增加属性

为了增加新属性,先单击增加…,从组合框中选择需要的属性名称,或者输入你自定义的名称,然后在下面的编辑框内输入取值。有多个取值的属性,例如忽略列表,肯呢个输入多行。单极确认将属性增加到属性列表。

如果你想一次性设置许多文件的属性,在资源管理器中选择文件/文件夹,然后选择上下文菜单属性

如果你想设置当前文件夹内的全部文件和文件夹,选中递归检查框。

一些属性,例如svn:needs-lock只能用于文件,所以它们在文件夹的属性下拉列表内不会出现。你仍旧可以递归的设置目录树中所有文件的属性,但是需要你自己输入属性名称。

如果你想编辑一个已有属性,在已有属性列表中选择它,然后单击编辑…即可。

如果你想删除已有属性,在已有属性列表中选择它,然后单击删除即可。

属性svn:externals可以用来下载位于同一版本库或不同版本库的其它工程。阅读“引用的工程”一节以获得更多信息。

Exporting and Importing Properties

Often you will find yourself applying the same set of properties many times, for example bugtraq:logregex. To simplify the process of copying properties from one project to another, you can use the Export/Import feature.

From the file or folder where the properties are already set, use TortoiseSVNproperties, select the properties you wish to export and click on Export…. You will be prompted for a filename where the property names and values will be saved.

From the folder(s) where you wi

sh to apply these properties, use TortoiseSVNproperties and click on Import…. You will be prompted for a filename to import from, so navigate to the place you saved the export file previously and select it. The properties will be added to the folders non-recursively.

If you want to add properties to a tree recursively, follow the steps above, then in the property dialog select each property in turn, click on Edit…, check the Apply property recursively box and click on OK.

The Import file format is binary and proprietary to TortoiseSVN. Its only purpose is to transfer properties using Import and Export, so there is no need to edit these files.

Binary Properties

TortoiseSVN可以处理文件的二进制属性。使用保存…到文件读取二进制属性值。使用十六进制编辑器或其它适当的工具创建文件,然后用从文件加载…设置二进制值为此文件的内容。

尽管二进制文件不经常使用,它们在一些程序中是有用的。举例来说,如果你存储了巨大的图形文件,或者用程序加载的文件巨大,你可能想将缩略图作为属性存储,于是你可以快速的预览。

提交属性

Subversion 属性是受版本控制的。在你改变或增加属性后必须提交。

属性冲突

如果因为其他用户已经提交了同样的属性,提交时出现冲突,Subversion 会产生一个 .prej 文件。在你解决冲突后,请删除此文件。

自动属性设置

你可以设置当文件和文件夹加入版本库时,自动设置属性。阅读“TortoiseSVN的设置”一节以获得更多信息。

TortoiseSVN Project Properties

TortoiseSVN 有自己专用的几个属性,它们都有tsvn:前缀。

  • tsvn:logminsize设置提交日志的最小长度。如果你输入的日志短于预设值,提交会被禁止。这个属性对于提醒你为每次提交提供一个适当的描述信息非常有用。如果不设置这个属性,或者设置为0,那么就允许空提交信息。

    tsvn:lockmsgminsize设置锁定日志的最小长度。如果你输入的日志短于预设值,加锁会被禁止。这个属性对于提醒你为每次加锁提供一个适当的描述信息非常有用。如果不设置这个属性,或者设置为0,那么就允许空加锁信息。

  • tsvn:logwidthmarker用 在要求日志信息被格式化为在最大宽度(典型是80字符)处换行非常有用。设置此属性为大于0的值会在日志消息对话框中做两件事: 放置一个标记指示最大宽度,和禁止自动换行,于是你可以看到输入的信息是否太长。注意: 这个特性仅在你选择的消息使用固定宽度字体时才能正确工作。

  • tsvn:logtemplate在需要定义日志消息格式化规则的工程中使用。在你开始提交时,这个属性的多行消息会被插入日志消息编辑框。你可以编辑它以便包含需要的信息。注意: 如果你使用了tsvn:logminsize属性,请确认这个长度大于模版的长度,不然就会失去其保护作用。

  • Subversion allows you to set “autoprops” which will be applied to newly added or imported files, based on the file extension. This depends on every client having set appropriate autoprops in their subversion configuration file. tsvn:autoprops can be set on folders and these will be merged with the user’s local autoprops when importing or adding files.

    如果本地 autoprops 与 tsvn:autoprops 冲突,项目设置优先(因为它们是针对此项目的)。

  • In the Commit dialog you have the option to paste in the list of changed files, including the status of each file (added, modified, etc). tsvn:logfilelistenglish defines whether the file status is inserted in English or in the localized language. If the property is not set, the default is true.

  • TortoiseSVN can use spell checker modules which are also used by OpenOffice and Mozilla. If you have those installed this property will determine which spell checker to use, i.e. in which language the log messages for your project should be written. tsvn:projectlanguage sets the language module the spell checking engine should use when you enter a log message. You can find the values for your language on this page: MSDN: Language Identifiers.

    你可以用十进制输入取值,如果用0x前缀的话,也可以用十六进制。例如英语(美国英语)可以输入0x0409或者1033

  • The property tsvn:logsummary is used to extract a portion of the log message which is then shown in the log dialog as the log message summary.

    The value of the tsvn:logsummary property must be set to a one line regex string which contains one regex group. Whatever matches that group is used as the summary.

    An example: [SUMMARY]:s+(.*) Will catch everything after “[SUMMARY]” in the log message and use that as the summary.

  • 当你想增加新属性时,你可以从组合框的下拉列表选取,也可以输入你喜欢的任何属性名称。如果你的项目使用了自定义属性,并且想让这些属性出现在组合框的下拉列表中(避免输入时拼写错误),你可以使用tsvn:userfilepropertiestsvn:userdirproperties创建自定义属性列表。对目录应用这些属性,当你编辑其任何子项属性时,你自定义的属性将会在预定义属性名称列表中出现。

Some tsvn: properties require a true/false value. TortoiseSVN also understands yes as a synonym for true and no as a synonym for false.

TortoiseSVN can integrate with some bug tracking tools. This uses project properties that start with bugtraq:. Read “Integration with Bug Tracking Systems / Issue Trackers”一节 for further information.

It can also integrate with some web-based repository browsers, using project properties that start with webviewer:. Read

>“与基于 WEB 的版本库浏览器集成”一节 for further information.

Set the project properties on folders

These special project properties must be set on folders for the system to work. When you commit a file or folder the properties are read from that folder. If the properties are not found there, TortoiseSVN will search upwards through the folder tree to find them until it comes to an unversioned folder, or the tree root (eg. C:) is found. If you can be sure that each user checks out only from e.g trunk/ and not some sub-folder, then it is sufficient to set the properties on trunk/. If you can’t be sure, you should set the properties recursively on each sub-folder. A property setting deeper in the project hierarchy overrides settings on higher levels (closer to trunk/).

For project properties only you can use the Recursive checkbox to set the property to all sub-folders in the hierarchy, without also setting it on all files.

When you add new sub-folders using TortoiseSVN, any project properties present in the parent folder will automatically be added to the new child folder too.

小心

Although TortoiseSVN’s project properties are extremely useful, they only work with TortoiseSVN, and some will only work in newer versions of TortoiseSVN. If people working on your project use a variety of Subversion clients, or possibly have old versions of TortoiseSVN, you may want to use repository hooks to enforce project policies. project properties can only help to implement a policy, they cannot enforce it.

引用:http://www.subversion.org.cn/tsvndoc/tsvn-dug-propertypage.html