Archive for web

有点无语哇,没有基础直接“跑”就是这种结果。前面写的一篇《django发送json数据并格式化datetime时间数据》,今天晚上用ie8突然发现哇T_T,Date没转换过来,急哭了。原因是前面测试很长时间是在ubuntu下做的,这次回家了,用的windows。发现测试很重要。 正题:是转换的日期字符串格式不正确。开始群里告诉我是浏览器的原因,还真找到几篇文章,但发现没用。重新搜索javascript 的Date参数格式,挨个在两个浏览器里的控制台测试,终于找到了。。。T_T泪奔~ 再次搜索python strftime 格式化,修改。测试。通过了~\(≧▽≦)/~ 记录: 几种datetime格式,测试了第一个,并使用:

var someDate=new Date("Month dd,yyyy hh:mm:ss");
var someDate=new Date("Month dd,yyyy")
var someDate=new Date(yy,mm,dd,hh,mm,ss)
var someDate=new Date(GMT milliseconds from 1/1/1970)
strftime 格式化记录,防止再次搜索: %a 星期几的简写 Weekday name, abbr. %A 星期几的全称 Weekday name, full %b 月分的简写 Month name, abbr. %B 月份的全称 Month name, full %c 标准的日期的时间串 Complete date and time representation %d 十进制表示的每月的第几天 Day of the month %H 24小时制的小时 Hour (24-hour clock) %I 12小时制的小时 Hour (12-hour clock) %j 十进制表示的每年的第几天 Day of the year %m 十进制表示的月份 Month number %M 十时制表示的分钟数 Minute number %S 十进制的秒数 Second number %U 第年的第几周,把星期日做为第一天(值从0到53)Week number (Sunday first weekday) %w 十进制表示的星期几(值从0到6,星期天为0)weekday number %W 每年的第几周,把星期一做为第一天(值从0到53) Week number (Monday first weekday) %x 标准的日期串 Complete date representation (e.g. 13/01/08) %X 标准的时间串 Complete time representation (e.g. 17:02:10) %y 不带世纪的十进制年份(值从0到99)Year number within century %Y 带世纪部分的十制年份 Year number %z,%Z 时区名称,如果不能得到时区名称则返回空字符。Name of time zone %% 百分号

Continue

用了一个javascript的插件,json传送过来的时间需要是Date对象类型的开始的思路就错了,以为可以直接返回这种类型的数据。差,天真了。后来又看到直接返回含有js语句的json文本,非常不喜欢。就想能不能转成js中Date能识别的格式,哈哈果然有。方法如下: 我用的 ,用json也行哇。views.py里的代码

from django.utils import simplejson

 li = []
    for a in articles:
        article={}
	article["start"] = a.datetime.strftime('%Y-%m-%dT%H:%M:%S')
        article["content"] = a.content
        li.append(article)
    json = simplejson.dumps(li)
    return HttpResponse(json)
其中strftime('%Y-%m-%dT%H:%M:%S') 是格式化为Date对象格式的字符串,方便在浏览器的javascript中转换为Date对象. html文件比较简单判断json的长度 循环变成Date对象,jsvascript
var i=0;
for(i=0;idjango发送json数据并格式化datetime时间数据风波二

Continue

以前写的乱了 ,这次转到ubuntu下,又重新装了一次。重新记录: 1.apache安装。apt-get install apache2 2.mod_wsgi安装。sudo apt-get install libapache2-mod-wsgi 3.mysql安装。sudo apt-get install mysql-server 4.python-mysql安装。sudo apt-get install python-mysqldb 5.django 安装 先到官网下载然后按照官网的方法安装 tar xzvf Django-1.3.1.tar.gz cd Django-1.3.1 sudo python setup.py install  

Ubuntu PIL安装Python Imaging Library

http://0x55aa.sinaapp.com/linux/307.html

ubuntu Apache+mod_wsgi错误提示

http://0x55aa.sinaapp.com/linux/303.html

Ubuntu安装配置Apache和mod_wsgi

http://0x55aa.sinaapp.com/linux/267.html

Ubuntu下mysql数据库的编码修改

http://0x55aa.sinaapp.com/linux/305.html  

Continue

在网上找到一篇文章《将django的管理端控件用到前端页面》,写得很详细。自己还需要时间的设定,就粗略的研究了一下django后台admin的时间控件的设置使用。 forms.py文件

from django.contrib.admin import widgets

linetime = forms.DateTimeField(required=True,label='时间',widget=widgets.AdminDateWidget())
head中增加如下代码
 
 



{{ form.media }}
{{ form.media }}添加了两条script语句。 body中添加{{ form.linetime }} widgets.py文件中的几个def: AdminDateWidget,这个是只有日期的时候,在froms中使用。 AdminTimeWidget,这个在只有时间。 AdminSplitDateTime,这个时间和日期都有,按情况选择就OK了。

Continue

models.py里的写法: link = models.ImageField(upload_to='pic/' ,blank=True,null=True) forms.py里的写法: image = forms.ImageField(required=False)# required = false 不是必填项 template里的写法: <form enctype="multipart/form-data" method='post' action='.'>{% csrf_token %} 其中生成的表单是name=“image”,见forms views.py里的写法,没写完,只是实现了上传和缩略图功能:

if request.method == 'POST':
        form = AddArticlesForm(request.POST,request.FILES)
        #如果用户提交的表单数据验证合法
        if form.is_valid():

        if 'image' in request.FILES:
                image = request.FILES["image"]
                #debug()
                img= Image.open(image)
                img.thumbnail((250,250),Image.ANTIALIAS)
                
                url='pic/'+image.name
                name= 'E:/django/plant/media/'+url
                img.save(name,"jpeg")
                pic = Pic(articles=newarticle,
                      link=url,
                      )
                pic.save()

Continue

MySQL-python for Windows Distributions http://www.codegood.com/downloads python下载地址被墙了,需要vpn,到ftp里面下载就不需要了: http://www.python.org/ftp/python/ python 2.6.7没有for windows版本。

Continue

查了些验证码的资料,选了一个比较简单的例子,参照着写得不是很好。找了一篇比较经典的收藏了。

TFontAngle=range($this->TFontAngle[0],$this->TFontAngle[1]);
		$this->TFontSize=range($this->TFontSize[0],$this->TFontSize[1]);

		$arr=array();
		$Chars=$this->Chars;
		$TFontAngle=$this->TFontAngle;
		$TFontSize=$this->TFontSize;
		$FontColors=$this->FontColors;
		$code="";
		$font=dirname(__FILE__)."/font/".$this->TFonts[0];

		$charlen=strlen($Chars)-1;
		$anglelen=count($TFontAngle)-1; // 角度范围
		$fontsizelen=count($TFontSize)-1; // 角度范围
		$fontcolorlen=count($FontColors)-1; // 角度范围

		for($i=0;$i<$this->Length;$i++) ///得到字符与颜色
		{
			$char=$Chars[rand(0,$charlen)]; ///得到字符
			$angle=$TFontAngle[rand(0,$anglelen)]; ///旋转角度
			$fontsize=$TFontSize[rand(0,$fontsizelen)]; ///字体大小
			$fontcolor=$FontColors[rand(0,$fontcolorlen)]; ///字体大小

			$bound=$this->_calculateTextBox($fontsize,$angle,$font,$char); ///得到范围

			$arr[]=array($fontsize,$angle,$fontcolor,$char,$font,$bound);  ///得到矩形框
			$code.=$char;
		}
		$this->Code=$arr; //验证码
		return $code;
	}

	public function Draw() ///画图
	{
		if(empty($this->Code)) $this->RandRSI();
		$codes=$this->Code; ///用户验证码


		$wh=$this->_getImageWH($codes);

		$width=$wh[0];
		$height=$wh[1]; ///高度

		$this->Width=$width;
		$this->Height=$height;

		$this->Image = imageCreate( $width, $height );
		$image=$this->Image;

		$back = $this->_getColor2($this->_getColor( $this->BgColor)); ///背景颜色
		imageFilledRectangle($image, 0, 0, $width, $height, $back); ///填充背景

		$TPadden=$this->TPadden;

		$basex=$this->Txbase;
		$color=null;
		foreach ($codes as $v) ///逐个画字符
		{
			$bound=$v[5];
			$color=$this->_getColor2($this->_getColor($v[2]));
			imagettftext($image, $v[0], $v[1], $basex, $bound['height'],$color , $v[4], $v[3]);
			$basex=$basex+$bound['width']*$TPadden-$bound['left'];///计算下一个左边距
		}
		$this->TLine?$this->_wirteSinLine($color,$basex):null; ///画干扰线
		header("Content-type: image/png");
		imagepng( $image);
		imagedestroy($image);

	}

	/**
	 *通过字体角度得到字体矩形宽度*
	 *
	 * @param int $font_size 字体尺寸
	 * @param float $font_angle 旋转角度
	 * @param string $font_file 字体文件路径
	 * @param string $text 写入字符
	 * @return array 返回长宽高
	 */
	private function _calculateTextBox($font_size, $font_angle, $font_file, $text) {
		$box = imagettfbbox($font_size, $font_angle, $font_file, $text);

		$min_x = min(array($box[0], $box[2], $box[4], $box[6]));
		$max_x = max(array($box[0], $box[2], $box[4], $box[6]));
		$min_y = min(array($box[1], $box[3], $box[5], $box[7]));
		$max_y = max(array($box[1], $box[3], $box[5], $box[7]));

		return array(
		'left' => ($min_x >= -1) ? -abs($min_x + 1) : abs($min_x + 2),
		'top' => abs($min_y),
		'width' => $max_x - $min_x,
		'height' => $max_y - $min_y,
		'box' => $box
		);
	}

	private function  _getColor( $color ) //#ffffff
	{
		return array(hexdec($color[1].$color[2]),hexdec($color[3].$color[4]),hexdec($color[5].$color[6]));
	}

	private function  _getColor2( $color ) //#ffffff
	{
		return imagecolorallocate ($this->Image, $color[0], $color[1], $color[2]);
	}

	private function _getImageWH($data)
	{
		$TPadden=$this->TPadden;
		$w=$this->Txbase;
		$h=0;
		foreach ($data as $v)
		{
			$w=$w+$v[5]['width']*$TPadden-$v[5]['left'];
			$h=$h>$v[5]['height']?$h:$v[5]['height'];
		}
		return array(max($w,$this->Width),max($h,$this->Height));
	}

	//画正弦干扰线
	private function _wirteSinLine($color,$w)
	{
		$img=$this->Image;

		$h=$this->Height;
		$h1=rand(-5,5);
		$h2=rand(-1,1);
		$w2=rand(10,15);
		$h3=rand(4,6);

		for($i=-$w/2;$i<$w/2;$i=$i+0.1)
		{
			$y=$h/$h3*sin($i/$w2)+$h/2+$h1;
			imagesetpixel($img,$i+$w/2,$y,$color);
			$h2!=0?imagesetpixel($img,$i+$w/2,$y+$h2,$color):null;
		}
	}
}
DEMO:
$rsi = new Utils_Caption();
$rsi->TFontSize=array(15,17);
$rsi->Width=50;
$rsi->Height=25;
$code = $rsi->RandRSI();
session_start();
$_SESSION["CHECKCODE"] = $code;
$rsi->Draw();
以上代码下载地址是:http://files.cnblogs.com/chengmo/caption_chengmo.zip 作者:chengmo QQ:8292669 出处:http://www.cnblogs.com/chengmo 本文版权归作者和博客园共有,欢迎转载,请务必添加原文链接。

Continue

下面是从网上找的的,先记录: 在apache1.3.x中, 使用gzip来对内容进行压缩. 在新版的apache2.x里, deflate模块代替了gzip模块,用于对内容进行压缩. 查看Apache是否有deflate这个模块,目录:/etc/apache2/mods-available/ 启用这个mod:sudo a2enmod deflate 编辑deflate的配置文件: /etc/apache2/mods-available/deflate.conf 官网:http://httpd.apache.org/docs/2.0/mod/mod_deflate.html 一篇关于Apache优化的文章:http://my.oschina.net/lamp2me/blog/15317 默认Apache是所有插件都启用了,在配置文件apache2中可以看到,就不用启用了。

Continue

上传到Ubuntu服务器后错误百出,报错Data truncated for column 'title' at row 1, Google搜索发现是编码不统一的问题。 查看数据库编码的方法: show variables like 'character%'; 修改/etc/mysql/my.cnf文件 找到客户端配置[client] 在下面添加 default-character-set=utf8 默认字符集为utf8 在找到[mysqld] 添加 default-character-set=utf8 默认字符集为utf8 init_connect='SET NAMES utf8' (设定连接mysql数据库时使用utf8编码,以让mysql数据库为utf8运行) 重启mysql服务:service mysql restart 再查看发现成为utf8了。 drop掉原来建立的数据库,新建一个utf8的: CREATE DATABASE ms_db CHARACTER SET utf8 ; 前台测试,不再报错。

Continue

ExtractionError: Can't extract file(s) to egg cache The following error occurred while trying to extract file(s) to the Python egg cache: [Errno 13] Permission denied: '/var/www/.python-eggs' The Python egg cache directory is currently set to: /var/www/.python-eggs Perhaps your account does not have write access to this directory? You can change the cache directory by setting the PYTHON_EGG_CACHE environment variable to point to an accessible directory. 解决办法: mkdir /var/www/.python-eggs chmod -R 777 /tmp/.python-eggs 设置PYTHON_EGG_CACHE环境变量 $ SetEnv PYTHON_EGG_CACHE /tmp/aaa/ ##(setenv是在cshell中用的,在bshell中用的是 export 不同的SHELL设置变量的方法很不一致: csh : set、setenv bash : set、export ksh : set、export或直接赋值 vim /etc/profile export PYTHON_EGG_CACHE=/tmp/.python-eggs source /etc/profile export PYTHON_EGG_CACHE=/tmp/.python-eggs env |grep egg chmod -R 777 /tmp/.python-eggs or # vi /root/.bashrc export PYTHON_EGG_CACHE=/tmp/.python-eggs ) 目录权限注意要是apache用户,或者简单点就777

Continue