2010年5月23日星期日

Django发送邮件(Gmail)


Django发送邮件极其简单:
配置:Django发送邮件依赖于setting.py中的EMAIL参数。
 
EMAIL_HOST = "smtp.gmail.com" 
EMAIL_HOST_PASSWORD = "Your password" 
EMAIL_HOST_USER = "YOUR@GMAIL.COM" 
EMAIL_USE_TLS = True

发送邮件代码:
from django.core.mail import send_mail
send_mail( 'subject',  'message',  'fromuser@gmail.com',  ['touser@gmail.com'],)

2010年5月15日星期六

Django中文乱码



Django的内核是UTF-8的编码方式所以要避免乱码请注意一下几点:
1.数据库中的编码方式要是UTF-8的。
2.代码中View里面的最上面加上
# -*-coding:utf-8 -*-
可以防止模板出现乱码
3.代码中出现中文字符的地方请在本代码文件的最上面加入
# -*-coding:utf-8 -*-
可以避免代码文件出现乱码。

参考http://www.python.org/peps/pep-0263.html

英文Ubuntu ibus不能自动启动的解决

让ibus开机时自动启动,如下操作:
System >> Preferences >> Startup Applications
Click the button: Add
Name: IBus daemon
Command: /usr/bin/ibus-daemon -d
Comment: start IBus daemon when Gnome starts

VirtualBox Port Forwarding

今天解决了VirtualBox的一个困扰我已久的问题。

我的host是Win7,guest是ubuntu10.04.

我想在host中很方便的使用ubuntu中的东西。例如,我要在linux中运行django,然后在windows中127.0.0.1,因为我不知道我的ubuntu的IP是多少,也不想去了解。把下列内容存为*.bat:

VBoxManage setextradata Ubuntu10.04 "VBoxInternal/Devices/pcnet/0/LUN#0/Config/django/Protocol" TCP
VBoxManage setextradata Ubuntu10.04 "VBoxInternal/Devices/pcnet/0/LUN#0/Config/django/GuestPort" 8000
VBoxManage setextradata Ubuntu10.04 "VBoxInternal/Devices/pcnet/0/LUN#0/Config/django/HostPort" 8000


VBoxManage setextradata Ubuntu10.04 "VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/Protocol" TCP
VBoxManage setextradata Ubuntu10.04 "VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/GuestPort" 22
VBoxManage setextradata Ubuntu10.04 "VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/HostPort" 2222

然后到C:\Program Files\Sun\VirtualBox运行该文件,ssh可以了,为什么不能127.0.0.1呢?原来,运行django必须:

python manage.py runserver 0:8000

为什么如此呢,因为“Note that the default IP address, 127.0.0.1, is not accessible from other machines on your network. To make your development server viewable to other machines on the network, use its own IP address (e.g. 192.168.2.1) or 0.0.0.0.”

参考资料:http://blogs.oregonstate.edu/paynez/2009/06/24/django-development-in-virtualbox
http://forums.virtualbox.org/viewtopic.php?f=8&t=19221&p=82997&hilit=django#p82997

2010年5月13日星期四

我的第一篇博客

可惜,我喜欢把我的博客放在这里,但是,我的同胞却看不到。