If you benefit from web2py hope you feel encouraged to pay it forward by contributing back to society in whatever form you choose!
  1. First download pyGravatar https://bitbucket.org/gridaphobe/pygravatar/src Put the gravatar.py in gluon/contrib (if you prefer you can do pip install pyGravatar )
  2. In your model import Gravatar

    try: from gravatar import Gravatar except ImportError: from gluon.contrib.gravatar import Gravatar

  3. If you are using scaffold app, edit default/user.html view

    {{extend 'layout.html'}} <h2>{{=T( request.args(0).replace('_',' ').capitalize() )}}</h2> <div id="web2py_user_form"> {{if 'profile' in request.args(0):}} <img src='{{=Gravatar(auth.user.email).thumb}}'> {{pass}} {{=form}}

  4. You now have a profile page like that: http://awesomescreenshot.com/0d66e8k05

So simple, in any page you want the user avatar you just need to use

<img src='{{=Gravatar(auth.user.email).thumb}}'>
<img src='{{=Gravatar('user@bla.com').thumb}}'>

You can go further and get the user profile bio from gravatar.com In default/user.html

{extend 'layout.html'}}
<h2>{{=T( request.args(0).replace('_',' ').capitalize() )}}</h2>
<div id="web2py_user_form">
{{if 'profile' in request.args(0):}}
    <img src='{{=Gravatar(auth.user.email).thumb}}'>
    <blockquote style='width:300px;'>
    {{try:}}
        {{=Gravatar(auth.user.email).profile['aboutMe']}}
    {{except Exception:}}
        No profile
    {{pass}}
    </blockquote>
{{pass}}

Then you get this: http://awesomescreenshot.com/0106e94c4

You can also get extra services your user have registered in gravatar in views default/user.html

{{if 'profile' in request.args(0):}}
    <img src='{{=Gravatar(auth.user.email).thumb}}'>
    <blockquote style='width:400px;'>
    {{try:}}
        {{=Gravatar(auth.user.email).profile['aboutMe']}}
        {{services = Gravatar(auth.user.email).profile.get('accounts',{})}}
        {{if services:}}
            {{=UL(*[LI(service['shortname'],' - ',A(service['url'],_href=service['url']) ) 
                 for service in services])}}
        {{pass}}
    {{except Exception:}}
        No profile
    {{pass}}
    </blockquote>
{{pass}}

and then: http://awesomescreenshot.com/0206eam01

Hope this can be usefull for somebody. Tell your users to register in gravatar.com

Comments (0)


Hosting graciously provided by:
Python Anywhere