javascript - Creating a custom widget using django for use on external sites - Stack Overflow

I have a new site that I am putting together and part of it has statistics for the site's users. I

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] /

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./articles/web_widget_jquery/

Share Improve this question asked Mar 13, 2011 at 14:46 ajtajt 1,3813 gold badges13 silver badges30 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

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

Give your users this to put on their websites.

<script type="text/javascript" src="http://mysite./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./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

Obey the one rule: Keep It Simple Silly!

I know it may be very web 1.0 but an iframe really is your best friend in this situation. A simple piece of code such as <script>document.write("<iframe src='yoursite./userwidget/" + username + "' height='30' width='150' />");</script> to inject an iframe at load time will save you a crapload of time writing jsonp async code and dom manipulators and making sure that all the elements you inject onto their page will be styled correctly on every different website and worrying about origin policies.

if you have your code plug in an iframe pointed at you page then:

  • The origin is you! you don't have to worry about it.
  • You can use django templates instead of js to construct the widget being shown.
  • their CSS won't mess with your presentation.
  • their js can't easily manipulate your stats ;)
  • This is exactly what iframes were designed to do.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744080975a4555286.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信