Sunday 13 July 2014

MongoDB with Django - Python

MongoDB with Django


Following packages required to setup mongodb with django , so download these things and install it.


Now create django project using django-admin and edit database settings in your settgings py file ,

DATABASES = {
   'default' : {
      'ENGINE' : 'django_mongodb_engine',
      'NAME' : 'my_database'
   }
}


Now start mongodb engine using mongod.exe  by providing database path in the comand.

C:\Program Files\MongoDB 2.6 Standard\bin>mongod.exe --dbpath D:\forblog\django-mongodb\testapp\db

Now just create simple view and manupulate database database with django's ORM.

Friday 11 July 2014

appengine - django with non rel database

Hello friends, today I am going to tell how to run django on appengine with non relational database.
I am assuming that you have installed appengine sdk, python and django.
I am using following versions,

Python – 2.7
Django- 1.5
Appengine – 1.9.6

Download following packages ,
Now create django project named testapp,
Now unzip all the above packages and paste in your django project , now your project should like this ,

Now download django-testapp from https://github.com/django-nonrel/django-testapp and unzip it elsewhere, and copy manage.py file from this to our projct root directory.It will prompt you to replace with previous one ? click on yes. Now copy settings.py , indexes.py  and app.yaml file from  this folder and and paste it to our root directory ,

Now our project  under directory testapp looks like ,
Now just cut and paste urls.py from testapp app.  And finally our project looks like ,

Now run django project using runserver command locally , you should see homepage for django project.

Now edit views.py from testapp add following contents ,

from django.http import HttpResponse
def home(request):
    return HttpResponse("Hello World")

and modify urls.py file to the following contents,

from django.conf.urls import patterns, include, url

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^$', 'testapp.views.home', name='home'),
)


Uploading it to appengine ,

First go to appengine.google.com and create a new application by specifying applicaition name and title.And click on create application , now edit your app.yaml file and edit application name , make sure that name application created is same as name in app.yaml.
Now final step ,Run following command and provide valid creadentials ,
python manage.py deploy
and follow instrucions.
Now see you app online <appname>.appspot.com

Monday 3 February 2014

How to create numbered markers in Google Map

Hello Everyone ,

Here is simplest solution for creating numbered markers for google map,

You can use any platform for this solution , here i am using django, python.
 

Create a view that takes integer as a parameter .


from django.http import HttpResponse
import PIL
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw

def map_imagepin(request,num):
  
    font = ImageFont.truetype("arial.ttf", 11)

    text = str(num)
   
    if len(text)==1:
        text_pos = (16,13)
    elif len(text) ==2:
        text_pos = (13,13)
    elif len(text)==3:
        text_pos = (11,13)
    tcolor = (255,255,255)
    img = Image.open(r"c:\icon-pin.png")
    draw = ImageDraw.Draw(img)
    draw.text(text_pos, text, fill=tcolor,font=font)
    response = HttpResponse(mimetype="image/png")
    img.save(response, "PNG")
    return response


Here is urls.py

urlpatterns = patterns('',
    url(r'^/image/(\d+)/$', map_imagepin),
)



This view takes image from local location and add label to it , and return it in to the response.
Use this url  http://127.0.0.1:8000/mapimg/111/


Here is my original image ,
 Here is the edited image ,