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 ,




Tuesday 27 November 2012

Google app engine 1st Application

Hello Everyone,

Now we are going to run our first application in google app engine.

1. Select new project from File Menu , new project as PyDev Google App Engine Project.

2. Specify the name of project "Helloworld"

3. Select google app engine ' s home directory usually /home/user/google_appengine

4. Create a new file named helloworld.py and add the following contents to it.



import webapp2
class MainPage(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('Hello, webapp2 World!')
app = webapp2.WSGIApplication([('/', MainPage)],
debug=True)


5. Now crate configuration file named app.yaml , add following contents to it.

application: helloworld
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /.*
  script: helloworld.app


6. Now right click on project , and click on run. You can see output at http://localhost:8080




Monday 26 November 2012

Hello Everyone ,

This blog belongs to Google app engine..

Installation and First Hello world in Google app engine.

Tools going to be used :

  1. Python 2.7
  2. Eclipse 3.7 + PyDev plugin
  3. Google App Engine SDK for Python 1.6.4 
First you have to install pydev plugins for Eclipse.

In Eclipse , menu, “Help –> Install New Software..” and put above URL. Select “PyDev for Eclipse” option, follow steps, and restart Eclipse once completed.

Now install Google App Engine SDK for Python .

Installation done , Congrates.