Django community: Django links RSS
This page, updated regularly, aggregates Django links from the Django community.
-
Django Ajax POST请求失败解决办法
Django到了1.2.5版本以上,Ajax POST请求的使用发生了一些改变,我们还记得前些版本1.2,Django加入了CSRF机制,大大增强了网络请求的安全性,正是因为这点,现在的Django对页面任何POST请求的数据进行跨域攻击保护。当你升级到1.2.5或更高版本后,你会发现以前1.2.4版本的Ajax POST脚本方法都不能用或者出错了,Django官网上提供了一个解决办法,加个JQuery ajaxSend事件,让你以前的Ajax Post脚本能重新正常使用起来。请看代码: $('html').ajaxSend(function(event, xhr, settings) { function getCookie(name) { var cookieValue = null; if (document.cookie && document.cookie != '') { var cookies = document.cookie.split(';'); for (var i = 0; i < cookies.length; i++) { var cookie = jQuery.trim(cookies[i]); // Does this cookie string begin with the name we want? if (cookie.substring(0, name.length + 1) == (name + '=')) { cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); break; } } } return cookieValue; } if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) { // Only send the token to relative URLs i.e. locally. xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken')); } }); 把以上的代码 放在你使用Ajax POST 请求的页面上。 参考文档: http://docs.djangoproject.com/en/dev/ref/contrib/csrf/#csrf-ajax http://docs.djangoproject.com/en/dev/releases/1.3/#csrf-exception-for-ajax-requests -
因此该方法无法转换为存储表达式
项目中用到了C# 的Linq,今天碰到一个问题,提示这样: LINQ to Entities 不识别方法“System.DateTime Parse(System.String)”,因此该方法无法转换为存储表达式。 代码大概是这样的: result = result.Where(a => a.h.CreationDate >= DateTime.Parse(dtStart)); 也就是说我在结果集中过滤 某个时间段的记录,参数dtStart是字符的类型(框架的需要),所以表达式中引用了一个静态方法DateTime.Parse,把时间字符转成时间类型了。 查了下MSDN文档,大量的文摘,耐不住性子了,直接观察了下提示错误,把 DateTime.Parse直接挪出来了,改成: sdt = DateTime.Parse(dtStart); result = result.Where(a => a.h.CreationDate >= sdt); 把时间转换动作挪出来了,Linq终于能正常执行了!我想在linq表达式中,是不是不能使用一些静态方法呢? -
Python图片处理模块
平时用Python做web开发,上传图片是难免的,但直接拿PIL的函数来处理,总感觉有点繁琐,能不能封装些功能函数,让web上传处理图片更简便些。看了壑塥峈的《使用PIL调整图片分辨率》,得到了启发,他写的模块,主要是方便本地图片的批量处理,所以在他原来的基础上修改了一下,让它在开发web中使用。 在Django中,很容易得到file控件的值,比如file = request.FILES.get("photo",None),我就从这里出发,开始修改,把类修改成接受file参数和路径参数path,然后通过传入图片尺寸的方法,处理并保存图片,最后返回上传图片名字列表list,不多说,看看Django的views用法便知:from django.conf import settings frommyproject.common.graphicsimportGraphics defindex(request,template_name='apptest/picprocessor.html'): template_var=dict() ifrequest.method=='POST': file=request.FILES.get("photo",None) iffile: path=os.path.join(settings.MEDIA_ROOT,'apptest') resizer=Graphics(file,path) template_var["filename"]=resizer.run_cut((150,100),(300,200),(50,50),) returnrender_to_response(template_name,template_var,context_instance=RequestContext(request)) 使用该模块,你仅仅需要传入上传地址(绝对地址)和HttpRequest.FILES,初始化一下里面的Graphics类,然后使用类的方法,指定需要的尺寸tuple即可返回上传图片的名字。 Graphics类里有几个方法 是外部调用的,分别是run_cut,run_zoom_w,run_zoom_h和run_thumbnail。 run_cut:是根据你提供的尺寸,对原图片进行剪切,原图片比例和你指定的尺寸比例不相等时,程序会以原图中心为准放大缩小剪切成你需要的尺寸,图片不会拉伸。需要传入一个或多个尺寸tuple,如(150,100),(300,200),(50,50) run_zoom_w:是根据你提供的宽度,等比列缩放。方法需要传入一个或多个宽度tuple,如 150,100,200,300run_zoom_h:是根据你提供的高度,等比列缩放。方法需要传入一个或多个高度tuple,如 150,100,200,300 run_thumbnail:是传统的缩略图方法,需要传入一个或多个尺寸tuple,如(150,100),(300,200),(50,50) 这三个方法,能把原图处理成多种尺寸规格,也就是说能同时处理并上传成 多张不同尺寸的图片。图片处理的时候,全部采用Image.ANTIALIAS抗锯齿的过滤属性,保存的图片质量暂时定在100,这些都是为了保证剪切图片的时候,最大降低失真度,这样出来的图片体积就稍微大些了。图片的名字组合方式:uuid+"_"+w+"_"+h.jpg,如:ae5c011e-5e98-11e0-96e6-001a6bd081a2-600-400.jpg 具体的实现方法:#coding:utf-8 """图片上传后端处理""" __modify__ = '2goo.info' __email__ ='nmgkjdxjsj@gmail.com' VERSION = "Graphics v0.1 build 2011-03-27" import os,Image,ImageFile,uuid class Graphics: def __init__(self,uploadedfile,targetpath): '''初始化参数''' self.uploadedfile=uploadedfile self.targetpath = targetpath def check_folder(self): '''检查目标文件夹是否存在,不存在则创建之''' if not os.path.isdir(self.targetpath): os.mkdir(self.targetpath) return self.targetpath def pic_info(self, img): '''获取照片的尺寸和确定图片横竖版''' w, h = img.size if w>h: return w, h, 0 #横版照片 else: return w, h, 1 #竖版照片 def comp_ratio(self, x, y): '''计算比例.''' x = float(x) y = float(y) return float(x/y) def pic_cut(self, image, p_w, p_h): '''根据设定的尺寸,对指定照片进行像素调整 图形不会变形 如果指定尺寸比例和原图比例不 相等时,最大范围剪切''' #获取指定照片的规格,一般是1024,768 img = image w, h, isVertical = self.pic_info(img) #判断照片横竖,为竖版的话对调w,h if isVertical: p_w, p_h = p_h, p_w #如果照片调整比例合适,直接输出 if self.comp_ratio(p_h, p_w) == self.comp_ratio(h, w): target = img.resize((int(p_w), int(p_h)),Image.ANTIALIAS)#hack:高保真必备! # ANTIALIAS: a high-quality downsampling filter # BILINEAR: linear interpolation in a 2x2 environment # BICUBIC: cubic spline interpolation in a 4x4 environment return target #比例不合适就需要对照片进行计算,保证输出照片的正中位置 #算法灵感来源于ColorStrom if self.comp_ratio(p_h, p_w) > self.comp_ratio(h, w): #偏高照片的处理 #以高为基准先调整照片大小 #根据新高按比例设置新宽 p_w_n = p_h * self.comp_ratio(w,h) temp_img = img.resize((int(p_w_n), int(p_h)),Image.ANTIALIAS) #获取中间选定大小区域 c = (p_w_n - p_w)/2 #边条大小 box = (c, 0, c+p_w, p_h) #选定容器 #换成crop需要的int形参数 box = tuple(map(int, box)) target = temp_img.crop(box) return target else: #偏宽的照片 #以宽为基准先调整照片大小 p_h_n = p_w * self.comp_ratio(h, … -
Nose Test Runner for Django
-
Amazon.com: Practical Django Projects (Expert's Voice in Web Development) (9781430219385): James Bennett: Books
-
Le Mad Blog
Un blogue qui parle assez souvent de Django. -
Problems with concurrent DB access and get_or_create() - Django developers | Google Groups
-
Pet Microchip for Dogs and Cats | HomeAgain Pet ID & Recovery Service
-
Django | URL dispatcher | Django documentation
-
Django and Bleach
-
ubernostrum / django-registration / wiki / Home – Bitbucket
-
Django | Model field reference | Django documentation
-
Djangoでjson出力 - kuma8の日記
-
第4章: テンプレート - The Django Book (日本語)
-
Python プログラマのための Django テンプレート言語ガイド — Django v1.0 documentation
-
Djangoの人はほんとに優しい ベーステンプレートの使い方 - a2c.get.diary
-
Django で CSV を出力する — Django v1.0 documentation
templateファイルの構成を確認する -
モデルの作成 — Django v1.1 documentation
-
django-imagehandler on Vimeo
-
Adding Views to the Django Admin | Beardy Geek
-
django-ajax-selects - jQuery-powered auto-complete fields for ForeignKey, ManyToMany and text fields - Google Project Hosting
-
Boston Companies Using Django | Bostinnovation: Boston Innovation, Start-ups and Tech News
-
python - What's the best solution for OpenID with Django? - Stack Overflow
-
Demand Generation | Online Video Marketing | Video Campaign
-
robhudson/django-debug-toolbar - GitHub
The Django Debug Toolbar is a configurable set of panels that display various debug information about the current request/response and when clicked, display more details about the panel's content.