django-nonrel on GAE

Experiment 01 - Django non rel First Steps

Well... djano non rel is a great project and it is quite easy to make it work with your google app engine account.


Prerequisites

Linux/unix knowledge 
(Not much )you should know at least how to move in directories and use few basic commands like mkdir, ls -a, uname -a, ln -s  ... please read them from here . It would take 25-30 min
$ is the Linux prompt


Environment Details

OS 
$ uname -a
Linux user.localdomain 2.6.34.7-56.fc13.i686 #1 SMP Wed Sep 15 03:33:58 UTC 2010 i686 i686 i386 GNU/Linux

python

$ whereis python
python: /usr/bin/python2.6 /usr/bin/python /usr/lib/python2.6 /usr/include/python2.6 /usr/share/man/man1/python.1.gz

hg -  
$ whereis hg
hg: /usr/bin/hg /usr/share/man/man1/hg.1.gz

if not installed then use
yum install hg

install svn too it may be required in future
yum install svn

Note : yum is for fedora

App Engine SDK (python)

googleappengine SDK installed under /home/google_appengine
Download it here for your OS,  unpack it usinng gunzip or any extractor , create ~/google_appengine using
$ mkdir google_appengine
and place the extracted files in  google_appengine/
[google_app_engine ]$ python setup.py install


Django
installed django (you can follow the process mentioned here) ,
however django nonrel team has customized it to app engine needs. please read it here.
if you want to learn it you this free book. It is quite amazing.
or purchase from amazon Complete Django The Ultimate Django Book (English and French Edition)


All django non rel apps cloned to ~/django-nonrel

[ django-nonrel]$ ls -la
                  dbindexer
                  django
                  djangoappengine
                  djangotoolbox

you can get the hg links here

A Mobile phone and Number
It will be used to verify account creation on App engine (Verification is one time)
Please see the list of Carriers supported by Google.


Here We are ready to try your hands on django-non rel, take a break , grab a cop of coffee
and enjoy!!



Steps followed by me were :-
1. I cloned the test app  from hg clone https://bitbucket.org/wkornewald/django-testapp
2. created symlinks from ~/django-nonrel directory as follows
  
     $ cd django-testapp
     change to /django-testapp
    [ django-testapp] $ ln -s <Yourpath/to>/django-nonrel/django django
    [ django-testapp] $ ln -s <Yourpath/to>/django-nonrel/djangoappengine/ djangoappengine
    [ django-testapp] $ ln -s <Yourpath/to>/django-nonrel/dbindexer/ dbindexer
    [ django-testapp] $ ln -s ../../django-nonrel/djangotoolbox/ djangotoolbox
  
     mark   is the space " "
     check if the symlinks have appeared
     using
  
     [ django-testapp ]$ ls -la

3. logged in my appengine account using http://appengine.google.com
    [I already have a google account ]
4. Created an app named djangotestapp - It is a very easy process
    explore yourself , or follow this tutorial on youtube

5. once an app id ( djangotestapp ) is created we need to modify few things in cloned django-testapp
    files , namely app.yaml and settings.py
    a) In app.yaml change application: ctst to application: djangotestapp
    b) (Optional ) change SITE_ID = 29 to SITE_ID = 1
6. Now use
    $ ./manage.py createsuperuser
      and create your super user details
    $ ./manage.py syncdb
     and sync the datastore
    $ ./manage.py runserver --- to run the appengine server locally
     and run the server  locally and point your browser to http://localhost:8000/
     You would see It Works! Page
7. Use  $ ./manage.py deploy
   to upload the application on appengine
8. point your browser to http://djangotestapp.appspot.com
   You would see It Works! Page

Now do something useful with django-nonrel and google appengine.:)

I have tried my best to cover everything in this post.
Please feel free to post questions or add details if i have missed any.

Cheers!!!





Experiment 02 - How to Enable Admin using Django non-rel  
on GAE
My reading resources are here

There are few modifications done by django non rel project team for automated deployment of your apps on GAE and they are indeed great.

$ ./manage.py deploy  command takes care of your deployment needs. Don't use it right now :)


A ) Create super user
However remember always that your local data store is not ported to GAE data store for your application. Therefore the createsuperuser command used in Experiment 01  has not resulted in creating any super user  on your production datastore. Then what should we do ??? how to create a super user in production datastore??
Here something called remote (magic word ) comes to rescue, which uses remote api to communicate with your production app's python shell.

so any command, which is there for ./manage.py, you want to execute on your production app , just use remote before the command. For example we can create a super user for our production app using
./manage.py remote createsuperuser , So Create the super user !!.

Quick Fix for admin logout error, which you are bound to encounter, till it is fixed for later release :) of non rel


access your production app's remote python shell and follow below mentioned steps

$./manage.py remote shell - It will open remote python shell on your GAE. I was quite happy to see remote python shell >>_

  >> from django.contrib.sites.models import Site
  >> import settings
  >> Site(id=settings.SITE_ID,domain="Urappid.appspot.com", name="djangotestapp")
>> Site.save()
fix it on local as well ,
just replace the 03rd line with below line after logging in local shell using $./manage.py shell
  >> Site(id=settings.SITE_ID, domain="localhost", name="djangotestapp")

  You can validate using the following line on both local and remote python shell
 >> Site.objects.all( )
You would see something like
[Urappid.appspot.com] -- on remote 

[local host] -- on local


B ) Configure Django Admin App
   (on the local app)
Django admin app is shipped with your django distribution it does not need seperate installation, you just need to enable it
   1. Change your UrlConf
   Add these lines for imports

   from django.views.generic.simple import direct_to_template

   from django.contrib import admin

   admin.autodiscover()


   Add the url pattern

    urlpatterns = patterns('',
        ('^_ah/warmup$', 'djangoappengine.views.warmup'),
        (r'^admin/', include(admin.site.urls)), # Enabling Admin 
        ('^$', direct_to_template,{'template': 'home.html'}),
    )



  2. Modify your  INSTALLED_APPS settings.py
     INSTALLED_APPS = (
                         'django.contrib.admin', # uncommented the line
                         '............',
     )

C) Sync the local db
   use
   $./manage.py syncdb
       You can run ./manage.py validate -- to check if all admin models are working fine before syncing db

D) Check it on local by running the server
    $./mange.py runserver
    point your browser to http://localhost:8000/admin
    you will see admin interface login page (i hope you remember your super user , userid and password :)

That's it , now use
$ ./mange.py deploy
wait for the update to finish ... finish your tea if you left it midway :)

After the update point your browser to http://<Urappid>.appspot.com/admin
You should be able to see the same admin login page as you saw on local

Enjoy!! Now you can use admin as you want !!

11 comments:

  1. Very informative, thank you.

    ReplyDelete
  2. thanks michael.. :) you are the first one to comment though..

    ReplyDelete
  3. and for me don't work -( all the time tells "Please enter a correct username and password."... on http://.appspot.com/admin

    ReplyDelete
  4. Hi Bogdan,

    Please create a super user for your appengine app using
    python manage.py remote createsuperuser , choose the default user or give a custom name and password.. remember if you are using django-testapp created by allbuttonspressed team , take care of adminlogout error too mentioned at http://stackoverflow.co/questions/4733498/admin-logout-error-in-django-nonrel.. Then you will able to access the django admin interface using http://urappid.appspot.com/admin

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. When i change the contents in urls.py on the webpage it gives an identation error/at admin
    unexpected indent (urls.py, line 4)

    ReplyDelete
  7. I've got this error while deploying

    I'm using GAE+ django nonrel

    Creating tables ...
    Installing custom SQL ...
    Installing indexes ...
    No fixtures found.
    Exception exceptions.AttributeError: "'NoneType' object has no attribute 'mkstemp'" in > ignored

    ReplyDelete
  8. Thank you very much for the info. I had tried everything to create a superuser in Admin django but nothing seems to work:

    python manage.py remote createsuperuser --username... Finally did it.

    ReplyDelete
  9. Anonymous12:35 AM

    jitendra, I uploaded django-nonrel along with the app. But now I have very little space for uploading my code files. I amm allowed 3000 files and most of space is taken up by django-nonrel. Is there any way that we could work it out by not including django-nonrel in the project?

    ReplyDelete
  10. Anonymous9:46 AM

    there is an error when i creating suepruser in local ..

    google.appengine.api.yaml_errors.EventListenerYAMLError: mapping values are not allowed here
    in "workspace/firstpoochedi/manage.py", line 3, column 4

    ReplyDelete

Please Leave your comments