May 14

几种不同的提供MEDIA_URL的方式。第一种是编写自己的简单模板标签:

yourApp/templatetags/media_url.py

from django.template import Libraryfrom yourapp.settings import MEDIA_URLregister = Library()

@register.simple_tagdef media_url():    return MEDIA_URL
引用:
{% load media_url %}<link href="{{ media_url }}css/main.css" rel="stylesheet" type="text/css">
 
更好的办法,是编写自己的context processor,或者直接使用django的,详细见官方文档:
http://docs.djangoproject.com/en/dev/ref/templates/api/#django-core-context-processors-media
  • Share/Bookmark
Sep 11

在Hostmonster上部署django的常见方法,可以参考文章http://www.frankdu.com/blog/archives/334

但是在实际部署中,发现很难按照那样的步骤成功部署。经过摸索,我的部署方法如下:

1. 安装Python 2.5
虽然django可以在python 2.3及以上版本运行,但是最推荐版本是2.5,在hostmonster的主机上提供的是2.3,所以你可以想安装python 2.5到个人目录中。基本安装步骤是,到www.python.org下载合适的安装版本。如果想从源代码安装,需要登陆SSH然后执行以下步骤:
a. 使用wget下载源代码
b. tar zxvf python-VERSION.tar.gz
c. cd python-VERSION
d. ./configure –prefix=$HOME
e. make
f. make install
如果安装缺乏软件,请按照提示安装。

2. 安装Django
最新的正式版本是django 1.0,以此为例安装步骤如下:
a. wget http://www.djangoproject.com/download/1.0/tarball/
b. tar zxvf Django-1.0.tar.gz
c. cd Django-1.0
d. export PATH=$HOME/bin:$PATH
e. python setup.py install

3. 设置路径
编辑“.bash_profile”文件,设置PATH和PYTHONPATH,以便程序能正确运行:
export PATH=$HOME/bin:$HOME/lib/python2.5/site-packages/django/bin:$PATH
export PYTHONPATH=$PYTHONPATH:$HOME/django/django_projects

然后重新加载.bash_profile,使新配置生效:
source ~/.bash_profile

4. 创建mysql数据库和账户等信息
在cPanel中创建用于django项目的mysql数据、数据库用户,并记录下来用于项目设置。

5. 创建项目目录
cd ~
mkdir django_projects
mkdir django_templates
cd django_projects
django-admin.py startproject mysite
chmod 600 mysite/settings.py
最后一句用于设置settings.py的文件权限,阻止其他用户看到我们的数据库、用户、密码等重要信息。修改settings.py的内容,设置数据库、时区、模板目录等信息。

6. 配置FastCGI
这一步非常重要!首先在cPanel的“Apache Handlers”中添加以下信息:
处理器名称:fcgid-script
文件扩展名:.fcgi

然后,在你想添加django支持的网站目录中,安装fcgi.py、django.cfgi,并加入URL改写规则。

wget http://svn.saddi.com/py-lib/trunk/fcgi.py
chmod 755 fcgi.py
下载文件并设置权限,如果文件无法下载,也可以使用链接http://www.frankdu.com/download/fcgi.py

在同一个目录,创建文件django.fcgi,文件内容如下:

#!~/bin/python
import sys, os
sys.path.insert(0,”/home/your-user-name/django/django_projects”)
from fcgi import WSGIServer
os.environ['DJANGO_SETTINGS_MODULE'] = ‘mysite.settings’
from django.core.handlers.wsgi import WSGIHandler
WSGIServer(WSGIHandler()).run()

要注意, 上面的”your-user-name”需要改为你在hostmonster的用户名。“django_projects”是咱们建立的放项目的目录. 第5行的”mysite”是Django生成的项目的名字. 这几个一定要对应!

同样要修改执行权限 :
chmod 755 django.fcgi

7. 配置Rewrite Rule
这个就简单了, 照葫芦画瓢! 和上面的django.fcgi文件在同一个目录下, 建立.htaccess文件(别把点忘了), 内容如下RewriteEngine On
RewriteBase /
RewriteRule ^(media/.*)$ – [L]
RewriteCond %{REQUEST_URI} !(django.fcgi)
RewriteRule ^(.*)$ django.fcgi/$1 [L]

8. 搞定

好了,到这里就搞定了!可以写一些测试脚本,放上来运行一下。我安装的域名是http://www.visafly.com,测试工作情况可以访问http://www.visafly.com/time/

  • Share/Bookmark
Sep 11

我个人的部署方案请访问http://www.frankdu.com/blog/archives/333

1. 事前准备
安装SVN(Hostmonster默认是不支持SVN的, 需要自己安装) ,开通SSH(Hostmonster里要想启用SSH, 竟然还需要身份证, 我ft…) , 并SSH上你的主机

因为之前已经在Hostmonster的主机上装好了SVN, 并且开通了SSH, 所以省了很多事情:)

2. 获取Django代码
在”~/”下建立”django_src”目录

mkdir django_src
进入目录

cd django_src
使用SVN获取代码

svn co http://code.djangoproject.com/svn/django/trunk/ ./

3. 设置路径
编辑”.bash_profile”文件, 设置PYTHONPATH 和 PATH路径, 方便使用Django

export PATH=$PATH:$HOME/django_src/django/bin
export PYTHONPATH=$PYTHONPATH:$HOME/django_src:$HOME/django_projects
重新加载”.bash_profile”文件, 来启用配置的路径

source ~/.bash_profile

4. 建立项目目录
在”~/”下建立”django_projects”目录

mkdir django_projects
进入目录

cd django_projects
建立Django项目”myproject”

django-admin.py startproject myproject
修改”myproject.settings”文件的权限, 限制只有你的用户可以读取, 这样可以避免同一个服务器上其他用户访问你的DB

chmod 600 myproject/settings.py

5. 配置FastCGI
这一步我花的时间最长! 关键是原文中的fcgi.py不能下载了, 我找了其他的fcgi.py又不对. 郁闷之下翻看了官方的文档, 官方文档说要用flup, 我又折腾了半天flup, 未果… 最后又Google了一堆fcgi.py, 终于被我找到一个对头的:)
现在BlueHost的控制面板里建立一个子域名(SubDomain), 建立成功后他会在你的”~/public_html”目录下建立对应的子域名的目录. 我建立了一个叫”lab”的子域名. 然后进入到这个目录中, 下载fcgi.py

wget http://svn.saddi.com/py-lib/trunk/fcgi.py
下载完毕后, 修改执行权限

chmod 755 fcgi.py
继续在这个目录下建立Django.fcgi, 文件内容如下

#!/usr/bin/env python
import sys, os
sys.path.insert(0,”/home/username/django_src”)
sys.path.insert(0,”/home/username/django_projects”)
from fcgi import WSGIServer
os.environ[’DJANGO_SETTINGS_MODULE’] = ‘myproject.settings’
from django.core.handlers.wsgi import WSGIHandler
WSGIServer(WSGIHandler()).run()

要注意, 上面的”username”需要改为你在BlueHost的用户名. “django_src”是最开始建立的放Django源代码的目录, “django_projects”是咱们建立的放项目的目录. 第6行的”myproject”是Django生成的项目的名字. 这几个一定要对应!

同样要修改执行权限, 这里再提醒一下大小写!

chmod 755 Django.fcgi
6. 配置Rewrite Rule
这个就简单了, 照葫芦画瓢! 和上面的Django.fcgi文件在同一个目录下, 建立.htaccess文件(别把点忘了), 内容如下

RewriteEngine On
RewriteBase /
RewriteRule ^(media/.*)$ - [L]
RewriteCond %{REQUEST_URI} !(django.fcgi)
RewriteRule ^(.*)$ django.fcgi/$1 [L]

7. 搞定
好了, 就酱紫了! 现在可以访问一下你新建的子域名了! 我建立的子域名是”lab”, 所以访问的就是 http://lab.v2nb.com . 看到django的字样了吧?
这里只是配置完了Web服务的部分, Django部分还需要自己再配置一下, 或者直接放上你自己的Django网站:)
如果有需要, 可以再修改.htaccess, 或者参考我参考的帖子:)

8. 参考

http://www.bluehostforum.com/showthread.php?t=715

http://wiki.dreamhost.com/index.php/Django#Setup

http://www.djangoproject.com/documentation/fastcgi/

http://blog.donews.com/limodou/archive/2006/03/29/799059.aspx

http://svn.saddi.com/py-lib/trunk/fcgi.py

来源:http://www.nickcheng.com/2007/01/23/build-django-site-on-bluehost/?pageid=1

  • Share/Bookmark
Jul 24

在GAE中遇到一个很怪的模块导入问题,问题的本质,就像Java的classpath设置一样,是python中导入模块时搜索路径的问题。比如站点目录结果如下:

mysite
├─__init__.py
├─models.py
├─templates [dir]
├─static [dir]
└─check [dir]
      ├─__init__.py
      ├─views.py
      └─models.py

在check目录的views.py中,我想引用根目录中的models.py中的函数,查看网页时,直接报错,找不到导入对

然后觉得很奇怪,就使用相对引用语法,应该能通过编辑器语法检查,但是在AppEngine无法运行。思前想后,应该是sys.path中,代码目录不在模块搜索路径中。搞之,修改django的启动文件,加入:

from os.path import dirname
import sys
sys.path.append(dirname(dirname(__file__)))

然后将sys.path的内容输出到浏览器,发现一个奇怪的现象:第一次访问时,上面加入的搜索路径是存在的;刷新浏览器后,新加的搜索路径莫明其妙的消失了!

万幸的是,根目录还是在搜索路径中的。所以只能灵活灵活地干活了:即将根目录的models.py,放到一个子目录中,然后使用from common.models import YourModuleObjectName,OK!

mysite
├─__init__.py
├─templates [dir]
├─static [dir]
├─common [dir]
│     ├─__init__.py
│     └─models.py
└─check [dir]
      ├─__init__.py
      ├─views.py
      └─models.py
  • Share/Bookmark
Jul 22

因为办签证,一直以来看了很多签证方面的资料,于是也在写一个签证信息网站,使用django网页开发框架,基于Google AppEngine的主机,还不错的。

总体感觉是,测试版本的产品,使用起来还真麻烦啊!google datastore和数据库使用习惯也不一样,还需要继续摸索。但django的模板系统很喜欢,方便、灵活而强大!同样原因,开发.NET的情况,也非常喜欢MonoRail框架和NVelocity模板系统,:)

嗯,那个签证网站地址是http://visa.appspot.com/

  • Share/Bookmark
Jan 13

背着笔记本,行走于宿舍和实验室之间,每次都要修改ip,一个要设成静态ip,另外一个是动态ip,小小研究了一下,写了两个脚本来做,以后不用点鼠标了,^_^

脚本lab.bat
netsh interface ip set address “LAN” dhcp
netsh interface ip set dns “LAN” dhcp

脚本lab.bat
netsh interface ip set address “LAN” static 59.66.96.123 255.255.254.0 59.66.96.1 1
netsh interface ip set dns “LAN” static 166.111.8.28 primary
netsh interface ip add dns name=”LAN” addr=166.111.8.29 index=2

其中LAN是我的局域网连接的名称。更详细的解释,参考netsh命令的帮助,或者查看“如何在命令行更改ip地址”。

如果对用Python完成这个工作感兴趣,请看“用Python干实事: 自动修改Windows的IP、网关和DNS设置”,呵呵。

  • Share/Bookmark
Oct 07

I want to find some technical books about laTeX. There are thousands of technical books on an internal campus FTP server.

Though I’ve written an ftp search eigine using lucene, I am still comfortable to build a pure list of books into a text file. Then just quick search through the text.

With other language, it may take a lot of time. But with python, I just did it in a couple of minutes. The sequence operation, file I/O, and ftplib are amazingly quick to use. Python is so lovely for small tasks. :)

  • Share/Bookmark
Sep 04

MetaWeblog API是一种给予XML-RPC的应用协议,类似于Atom APILiveJournal API,我们可以通过MetaWeblog API来远程发布、修改日志。Windows Live Spaces提供了对MetaWeblog API的支持,我们可以通过Windows Live Writer来发表日志,也可以通过编程的方式来发表日志。在开始之前,需要一些步骤来启动这个API:

  1. 如果没有Passport帐号,请在www.passport.com注册并获得一个。
  2. 如果还没有space,请在spaces.live.com注册并获得一个。
  3. 登录进入你的space,然后打开Settings(设置),然后打开Email Publishing(电子邮件发布)选项。
  4. 打开电子邮件发布功能。
  5. 选择一个Secret Word。

好了,现在可以开始使用MetaWeblog了,你的用户名就是你space的名字,你的密码就是Secret Word,使用API的地址就是:

https://storage.msn.com/storageservice/MetaWeblog.rpc

如果想了解更多关于这个API的资料,可以参见微软的文档《Windows Live Spaces MetaWeblog API》。

如果想了解如何使用C#、VB.NET开发日志发布程序,请参见MetaWeblog API Code Samples

如果想了解如何使用Python来操作MetaWeblog API,请参见Python文档中的xmlrpclib模块。其实总体来说,还是觉得用Python写最方便。

  • Share/Bookmark
Aug 31

发信人: MetalSlugX (Nil), 信区: Python

其实就一环境变量的问题,可以有如下选择
Stand alone Python for Windows
http://arctrix.com/nas/python/standalone.html

Movable Python (居然是商业的)
http://www.voidspace.org.uk/python/movpy/

或者
http://blender.bokee.com/5396621.html

www.portablepython.com/site/home/

  • Share/Bookmark
Aug 26

换了新的虚拟主机,有很多文件需要迁移,其中一个问题是通过FTP把原服务器的文件夹拷贝过去。由于速度的原因,最好直接服务器对服务器拷贝。用FlashFXP的服务器对传发现不行,只好到处找批量下载的shell脚本或者程序。

无果之下,准备动手写一个,用Python中的ftplib模块。这时忽然发现Python安装目录的子目录[Tools/Scripts]里面,有一个ftpmirror.py!Hallelujah!正是我要找的!

它的使用文档是:

Mirror a remote ftp subtree into a local directory tree.

usage: ftpmirror [-v] [-q] [-i] [-m] [-n] [-r] [-s pat]
[-l username [-p passwd [-a account]]]
hostname[:port] [remotedir [localdir]]
-v: verbose
-q: quiet
-i: interactive mode
-m: macintosh server (NCSA telnet 2.4) (implies -n -s ‘*.o’)
-n: don’t log in
-r: remove local files/directories no longer pertinent
-l username [-p passwd [-a account]]: login info (default .netrc or anonymous)
-s pat: skip files matching pattern
hostname: remote host w/ optional port separated by ‘:’
remotedir: remote directory (default initial)
localdir: local directory (default current)

比如,复制远程服务器ftp.somewhere.com上/huashi文件夹及所有内容,保存到本地的/home/frank/huashi下,命令是:

python ftpmirror.py -v -l ftp_login_name -p ftp_password ftp.somewhere.com /huashi /home/frank/huashi

剩下的就是等待了,:D

  • Share/Bookmark