Wednesday, February 02, 2011

Using django registration on django non rel


On my journery to learn django non-rel and app engine, I could make django registration work on GAE.  I did not have to change anything in the source, except few tweaks suggested by posts from django non-rel team on google groups. Please refer my reading list

This exercise was quite interesting as it introduced me to new concepts such as csrf and I18N.

There are many tutorials over the internet on django-registration, however I was a little confused.. Which one should I try first?. Then I homed on to few (list) and decided to go for it.

These are the steps which I followed.

1. cloned django-registration to any directory ( in our case we made it in django-nonrel )
  
$: hg clone https://bitbucket.org/ubernostrum/django-registration/

     

     created symlink in my project root folder
  
$: ln -s '.pathtoyourcloned_rep.'/django-registration/registration/   registration

   
2. Arranging the templates
    django-registration does not provide you with default templates, therefore we have to make our own. I downloaded them from here.
3. I created the registration directory under templates directory
  
     mkdir projectroot/templates/registration

     

    and copied all downloaded html templates in the templates/registration directory

4. Making an additional template
    The source for template did not have one (registration/activation_complete.html)
    therefore I created one
    activation_complete.html
    Just make a copy of registration_complete.html and save it as activation_complete.html
   Then I saved the template in the templates/registration directory

5. Accommodating to html Templates
    You need to take care of 02 things in the templates.
    a) crsf tags are not there in templates, go one by one in all templates, whereever you see form POST requests include csrf tokens '{% csrf_token %}'
    b) These templates inherit the contents of base.html so I made a change in my base.html and included the   div (id=header , from base.html from the downloaded templates), You can replace your base.html with the downloaded one.
    c) ensure that all your templates have '{% load i18n %}' tags for internationalization
6. Modified Settings.py
    I modified my settings.py as below
    a) included an import from djangoappengine
    
from djangoappengine.utils import on_production_server, have_appserver

      

    b) email settings so that email_send works properly
     ACCOUNT_ACTIVATION_DAYS = 2

    LOGIN_REDIRECT_URL = '/'

if on_production_server:

    EMAIL_HOST = 'smtp.gmail.com'

    EMAIL_PORT = 587

    EMAIL_HOST_USER = 'gmailusername@gmail.com'

    EMAIL_HOST_PASSWORD = 'xxxxxxx'

    EMAIL_USE_TLS = True

    DEFAULT_FROM_EMAIL = 'gmailusername@gmail.com'

    SERVER_EMAIL = 'gmailusername@gmail.com'  

   

else:

    # local

    EMAIL_HOST = 'localhost'

    DEFAULT_FROM_EMAIL = 'webmaster@localhost'


   c) inlcuded i18N support
  
 USE_I18N = True 

   d) included CsrfViewMiddleware
  
    MIDDLEWARE_CLASSES = (

    'django.middleware.common.CommonMiddleware',

    'django.contrib.sessions.middleware.SessionMiddleware',

    'django.contrib.auth.middleware.AuthenticationMiddleware',

    'django.contrib.csrf.middleware.CsrfViewMiddleware',

     )

     
7. modified UrlConf
 Then I modified my UrlConf for proper url directions

urlpatterns = patterns('',

    ('^_ah/warmup$', 'djangoappengine.views.warmup'),

    ('^admin/',include(admin.site.urls)), #enabling the admin

    ('^$', direct_to_template,

     {'template': 'home.html'},'home'),# or you can use 'index' if you are using downloaded base.html

    ('^home/$', direct_to_template,{'template': 'home.html'}),

    (r'^accounts/', include('registration.urls')), # Working

)


8. django-registration tweak
   django-registration uses "_iexact" filters and the app engine uses "exact" therefore you need to modify the forms.py in django-registration installed directory (we cloned it )
  forms.py
  
from dbindexer.api import register_index

register_index(User, {'username': 'iexact', }) 

   

Note: I am scared if this tweak will remain if i update the cloned django-registration from its source next time. please add your comments on this.

Then run suncdb using

$ ./manage.py syncdb


Test it on local using
$ ./manage.py runserver


Then deploy your application on GAE using
$ ./manage.py deploy


I hope everything works for you.
please let me know if you face any difficulty.
Enjoy !!! django norel with django-registration on GAE

2 comments:

  1. Anyway to use an SMTP sink to monitor your emails? What port does the default email settings use?

    ReplyDelete

Please Leave your comments