存档

文章标签 ‘django’

DJANGO FORMS:This field is required

2010年12月27日 评论已被关闭

when meet This field is required at django forms, we can add “required=False” at Fields.

from django import forms

class ContactForm(forms.Form):
    subject = forms.CharField(max_length=100)
    message = forms.CharField()
    sender = forms.EmailField()
    cc_myself = forms.BooleanField(required=False)

分类: python 标签:

nginx+Django+memchached环境搭建备忘

2009年8月14日 评论已被关闭

系统环境
Kernal:Linux 2.6.9-78
nginx-0.7.61
pcre-7.9
Python version: 2.5.4
Django-1.1-py2.5
mysql-5.0.84
flup-1.0.2-py2.5
MySQL_python-1.2.3c1-py2.5-linux-i686
python_memcached-1.44-py2.5
setuptools-0.6c9-py2.5

1、nginx、python、mysql的安装可参考官方及网上的安装文档

2、安装django  http://www.djangoproject.com/
wget http://media.djangoproject.com/releases/1.1/Django-1.1.tar.gz
tar zxvf Django-1.1.tar.gz
cd Django-1.1
python setup.py install

3、Django以fastcgi方式启动需要
python-flup
wget http://www.saddi.com/software/flup/dist/flup-1.0.2.tar.gz
tar zxvf flup-1.0.2.tar.gz
cd flup-1.0.2
python setup.py install

4、MySQL-python-1.2.3c1.tar.gz
下载地址: http://sourceforge.net/projects/mysql-python/files/

我在安装和配置中遇到的问题:
1、mysql数据库连接:
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: libmysqlclient_r.so.15: cannot open shared object file: No such file or directory
我曾经装过了mysql5.2,发现MYSQL_HOME/lib中没有libmysqlclient_r.so.15,倒是有libmysqlclient_r.so.16,可能是因为mysql版本比较高的缘故,或者是因为dj版本比较低??
所以我选择了mysql5.0
tar MySQL-python-1.2.3c1.tar.gz
cd MySQL-python-1.2.3c1
python setup.py build

会有如下报错:
_mysql.c:2516: error: `v’ undeclared (first use in this function)
_mysql.c:2527: error: `name’ undeclared (first use in this function)
_mysql.c:2528: error: `self’ undeclared (first use in this function)
error: command ‘gcc’ failed with exit status 1
解决办法是:
vi site.cfg
# The path to mysql_config.
# Only use this if mysql_config is not on your PATH, or you have some weird
# setup that requires it.
mysql_config = /home/2hei.net/mysql/bin/mysql_config
然后接续
python setup.py build
python setup.py install

2、实际中还有这个错误出现:
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: /lib/tls/libc.so.6: version `GLIBC_2.4′ not found (required by /home/yujingtao/.python-eggs/MySQL_python-1.2.3c1-py2.5-linux-i686.egg-tmp/_mysql.so
解决办法是:
vi /etc/ld.so.conf
add /home/2hei.net/mysql/lib
#ldconfig /etc/ld.so.conf

3、新建立一个Django的webapp
django-admin.py startproject myweb

4、Django的启动
#django作为独立启动
django manage.py runserver method=threaded 127.0.0.1:8080

#以FastCGI方式启动
python manage.py runfcgi method=threaded host=127.0.0.1 port=9000
因为我的Django是跟nginx配合使用的,所以普通用户在内网监听大于1024的端口即可
因为每次更改urls.py都需要重启一下fastcg,为了方便使用我写了一个脚本:

#!/bin/bash
#script-name: start_myweb.sh
#wirte by: 2hei at 2009/08/12
cd /home/2hei/djproject/
if [ $# -lt 1 ];then
echo “Usages: sh start_myweb.sh [start|stop|restart]”
exit 0
fi
if [ $1 = start ];then
  isrun=`ps aux|grep “manage.py runfcgi”|grep -v “grep”|wc -l`
  if [ $isrun -eq 1 ];then
    echo “dj has running!”
    exit 0
  else
    /home/python/bin/python myweb/manage.py runfcgi method=threaded host=127.0.0.1 port=9000 –settings=settings
  fi
elif [ $1 = stop ];then
  djid=`ps aux|grep “manage.py runfcgi”|grep -v “grep”|awk ‘{print $2}’`
  kill -9 $djid
elif [ $1 = restart ];then
  djid=`ps aux|grep “manage.py runfcgi”|grep -v “grep”|awk ‘{print $2}’`
  kill -9 $djid
  /home/python/bin/python myweb/manage.py runfcgi method=threaded host=127.0.0.1 port=9000 –settings=settings
else
  echo “Usages: sh start_myweb.sh [start|stop|restart]”
fi

5、关于nginx解析Django静态文件的处理
nginx.conf

        location /media/ {
                root /home/2hei.net/djproject/myweb;
                break;
        }

cp -r /home/python/lib/python2.5/site-packages/django/contrib/admin/media/ /home/2hei.net/djproject/myweb/
Django管理界面:
dj-admin.JPG

6、因为需要用到memcache,所以memcach与Django进行了结合:
urls.py
urlpatterns = patterns(”,
    (r’^admin/’, include(admin.site.urls)),
    (r’^status/cache/$’, ‘myweb.memcached_status.view’),

settings.py
CACHE_BACKEND = ‘memcached://127.0.0.1:11211/’

dj-memcache.JPG

—————end——————-

分类: others 标签: ,