Sunday, 8 September 2013

crop image before saving it in DataBase on Django

crop image before saving it in DataBase on Django

So I upload file with the simple UploafFileForm and if form is valid I
save instance on disk and redirect it with url source link to some crop
view:
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
src = handle_uploaded_file(request.FILES['file'])
url = reverse("account:crop_avatar", kwargs=
{'auth_user_id':request.user.id, 'src': src})
return HttpResponseRedirect(url)
else:
form = UploadFileForm()
context = {}
context['form'] = form
return render_to_response('account_avatar.html', context,
RequestContext(request))
key 'src' contains url on uploaded image, for example /media/temp/001.jpg
After that I catch it urlpattern and run view function
url(r'^(?P<auth_user_id>\d+)/crop_avatar(?P<src>)', 'crop_avatar',
name='crop_avatar' ),
the problem is my view could not see src variable:
def crop_avatar (request, auth_user_id = None, src = 'somesource.jpg'):
return HttpResponse (src)
output nothing. What I do wrong?
p.s I have I little progress. I make src little bit simple removing path
and add regexp:
url(r'^(?P<auth_user_id>\d+)/crop_avatar/(?P<src>[^/#?]+)+\.(?:jpg|gif|png)$',
'crop_avatar', name='crop_avatar' ),
So the ploblem is in my regexp. I have two of them at the moment. First of
all it became throw error on when fisrt view redirect httpresponse and
patterns works only when I put src manuaaly in qierystring like :
http://blogondjango.com/profile/1/crop_avatar/cat.jpg
second problem is that he gives me only name without dot and last three
letters: so src is tha moment equals to 'cat' and not 'cat.jpg' which I
really need

No comments:

Post a Comment