Skip to main content

Posts

Showing posts from June, 2011

Django: how to generate json response.

It's a common practice to generate content different from standart HTML output by HttpResponce. Lets consider Example on how to generate JSON response by Django. First of all! Django has built in "other response generators". Most common task is to generate JSON. In general it's extremely easy. Operation called "seriliazation". Official Django docs say: "  Django’s serialization framework provides a mechanism for “translating” Django objects into other formats. Usually these other formats will be text-based and used for sending Django objects over a wire, but it’s possible for a serializer to handle any format (text-based or not). " Anyway an example, helped me a lot. from django . utils import simplejson def some_view ( request ):     result = []    result . append ({ "user" :request.user })     result . append ({ "key" :request.session.key })     return HttpResponse ( simplejson . dumps ( result ), mimetyp...

Django: Rotate Image with PIL usage.

Let's assume we have this simple model: from django.db import models class Image(models.Model): image = models.FileField(upload_to="images/")   Task is to take this simple image, rotate it and save instead of original. Let's discover this way to do so: #views.py from PIL import Image as PilImage from models import Image def rotate(request): #getting instance of the model item=Image.objects.get(pk=1) #opening image for PIL to access im = PilImage.open(image.image) #rotating it by built in PIL command rotated_image = im.rotate(270) #saving rotated image instead of original. Overwriting is on. rotated_image.save(item.image.file.name, overwrite=True) return HttpResponse(str(image.image)) This pattern will work in most cases.  Instead of rotate you can use any other PIL editing method, as for e.g. flip or sharpen the image... Have fun coding and please leave me a comment if you'll find this info useful. ...

SSH: Mount remote file system on your Mac over SSH to access in Finder

    Maybe you're already know about how to connect to remote servers through Local network on your Mac, but how about SSH server connections?     Lets say you may open a terminal window and run "ssh username@server.com" it will ask you for a password and "Ta-da" we're in. But what about usability. If you're console geek, this is enough for you. As for the others let's talk about adding some GUI to it. Some time ago Google engineers released a package for Mac's to mount remote filesystems, using lots of methods, including SSH.     This software is called MacFuse. It connects remote filesystems to your Mac's finder. You will see it in your computer as a regular Mac's network drive.     MacFuse has developed to the point, where it's extremely easy to "make it work". This is the way:     1. Install MacFuse and SSHFS. at the time of writing this article version is  MacFUSE-2.0.3,2.dmg  and you can  dow...

Django: Installing Eclipse and PyDev for django

In most common cases of programming Django with Eclipse are that Django is installed in system, like Python. I dislike this method, because it makes confusions for beginners. What Django version I'm in. Why did I download an app "X" and added an Eclipse project. Everything seems to be fine, but I cant install proper dependencies... So it's not a secret. Managing Django distributions is a hard work for junior developer. Let's try to make their work simpler, by this article. I'll try to show you the right way to install Django environment on Eclipse. 1. First of all let's install Eclipse from official site . I chose "Eclipse IDE for JavaScript Web Developers". Mac 32bit version, because I'm using this OS type. Any other distributive will work just fine, I think. 2. Unpack downloaded archive into some dir. It will be installation directory of the eclipse. Usually it doesn't mater where it is stored. So I used "/Users/garmoncheg/De...

Django: Better way to install django apps into apps. (Handling django projects dependencies.)

Let's talk about common programming practice. You love to code yourself. Everyday you write tons of code. Making lots of common tasks is interesting and challenging. No? A-ha! You love plugins too! :) I'm making a startup. Everyday there are some ideas coming to my head. Some are easy to implement, some are not, but I often download tons of plugins. There are lots of ways to use plugins without installing them to main system. Python virtualenv is easy and common to use for this purposes. Soon I'll write an article about it too. But the main problem of Python Virtualenv is it's complexity. You basically need to install something, make an environment and so on... What if you just need to implement nice and easy "Django way". Here direct copy comes in mind. It's the easiest way, as for me. Basic concept is:    - download plugin (be it in egg, zip, tar.gz or any other format)    - unpack it     - copy to django app dir ...

Django: add image in an ImageField from image url

Today I had experience with django file downloads from an specified url into Django model FileField. I'm writing content grabber from Flickr. I didn't find information about it and decided to write what I've discovered so far in example. Stackoverflow helped me a bit. But answer proposed there had not worked. So this is code example how to workout this issue: because code worth a thousand words :) We have a model with one field: #models.py class Photo ( models . Model ):     image = models . ImageField ( 'Label' , upload_to = 'path/' ) We need to create a photo from image url and save it to the model's FileField. #somewhere in views.py # usually in header imports from urlparse import urlparse import urllib2 from django . core . files import File   #add imprt of content file wrapper from django . core . files . base import ContentFile # somewhere: for e.g. in view handling the file operations photo = Photo () img_url =...