Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a custom widget using django for use on external sites

I have a new site that I am putting together and part of it has statistics for the site's users. I would like to create a widget that others can use on another website by invoking javascript that reads data from my server and shows that statistics for a given user, but I am having a hard time finding specific tutorials that covers this in django.

I have seen the link at Alex Maradon's site [0], but it looks to me like that is passing html back to the widget and I am having a hard time figuring out how to do this using something like xml.

Are there any django apps for doing this or does anyone know of good how-tos?

[0] http://alexmarandon.com/articles/web_widget_jquery/

like image 201
ajt Avatar asked Jan 20 '23 06:01

ajt


1 Answers

This is not matter of Django, you can solve this by using the most common solution. Javascript.

Give your users this to put on their websites.

<script type="text/javascript" src="http://mysite.com/widget/user/124546465"></script>

On a django view, render the next template:

(function(){
document.write('<div class="mysite-userprofile">');
document.write('My visits are {{total_visits}}<br />') 
document.write('</div>') })()
)

So on your view, you may have something like this, the mimetype is important

def total_visits(request, user_id):
    user = get_object_or_404(User, id = user_id)
    total_visits = Visits.objects.filter(user:user).total_visits() #this is a method to count, you may have to write your own logic
    context = {'total_visits': total_visits}
    render_to_response('widget_total_visits.html', context, mimetype='text/javascript')

What can you do next?

User settings, like this.

<script type="text/javascript">
      mysite_options = {
         'just_friends': True,
         'theme': 'bluemarine,
         'realtime': True
      }
</script>
<script type="text/javascript" src="http://mysite.com/widget/user/124546465"></script>

So on your template, you can use the variables set before include the script on the web site of your user, a simple stuff.

Later, you can use POST method, to gather information from the user clients. For stats.

And of course make it Ajax!

I hope this give you a path to follow

like image 142
Mario César Avatar answered Jan 24 '23 00:01

Mario César