Jun 30th, 2007 by viestards
Have not posted for some time. Mosly because I was offline, in place where are no internet connection available, taking part in strawberry collection. Weather was pretty hot and sunny and I had a good time swimming and sunbathing besides working. Overall it as nice not to sit in front of computer every day and I ate a whole lot of strawberries.
For next few days I’ll try to write some more posts, I started to learn Joomla! CMS and it is pretty interesting.
Posted in Uncategorized | Comments Off
Jun 6th, 2007 by viestards
I made a simple Django project, that shows how to implement helpers. It consists of two examples: first is ping pong example, which shows how to implement link_to_remote function, and second is AJAX Form example- it demonstrates how to use form_remote_tag.
You can download it from Google Project page.
Posted in programming, django, javascript, helpers, AJAX, prototype | Comments Off
Jun 6th, 2007 by viestards
Prototype’s AJAX.Update seems to be nice, but it has problem, that it updates target div. Django usual practice is to show error description above form field, and not somewhere below. This is a problem, that can not be solved easily with helpers. I wonder how RoR does it, Google has not been very helpful here.
For now, errors are written in response div, it’s not so user-friendly.
Posted in programming, django, helpers, AJAX, prototype | Comments Off
May 25th, 2007 by viestards
Ok, today I put helpers on Google code under new BSD licene. Project page is here.
For now it supports:
helper- generic function, that can call any webhelpers.rails.protype function
visual_effect
remote_function
form_remote_tag
submit_to_remote
periodically_call_remote
link_to_remote
observe_form
observe_field
Posted in programming, django, helpers, AJAX, prototype | Comments Off
May 24th, 2007 by viestards
There is problem with link_to_remote function. While it is nothing more than remote_function with <a href="#"
thingies, it produces some wierd error because Django does not uses routes. In the same time remote_function works ok. Looks like I just add HTML stuff at the end.
Posted in programming, django, javascript, helpers | Comments Off
May 24th, 2007 by viestards
I started to port all helpers stuff to Django, but it’s mainly boring. Then the idea came to me, what if there will be only one (or a little bit more) which handle all calls to webhelpers.
So, there is one function, that can call any Pylons webhelpers prototype function- I called it helper. The idea is that every function is very similar and repeating every function call is turning more into copy/paste. But there is easy with getattr.
Result looks like this:
Generic helper <a href="#" onclick="{% helper function_name:remote_function url:/frets/shell/ update:pongbox success:test_funct() %}; return false;">here</a>
Just put it into your template and create pongbox div.
Templatetag file is available here, it is unclean, because I’m working on it, but it should work. Put into templatetags folder.
Posted in programming, django, helpers, AJAX, prototype | Comments Off
May 19th, 2007 by viestards
Have not written something in a while, was busy with one of my projects- porting Pylons webhelpers to Pylons. I didn’t know what to write, because there were no big things to write about.
Thinking of which I decided to start writing my project diary- all my problems, what I find out, hoping that others will find it useful. I decided to write at least three times a week, so this is first time.
When porting webhelpers, first problem was to create it the way that only the small part of code has to be rewritten. There is one problem thou, Pylons is using routes as their URL mapper, while Django uses regexps as URL mappers. My problem is that webhelpers escapes all apostropes because there has to be no such characters in URL! So I have a choice- rewrite parts that are responsible for urls (call to routes) or return direct value passing Template class, which is not usggested by Django team. For now I think, that best will be latter metod but just as proof of concept.
Posted in programming, django, helpers, AJAX, prototype | Comments Off
May 7th, 2007 by viestards
Long time Django was considered as AJAX-unfriendly web framework, which does not allow JavaScript code generation. On other hand , Pylons ported Rails helpers for their framework as webhelpers.
So maybe I can use Pylons templatetags in Django? Sounds hard, because Pylons use heavy Python coding in their templates, but you underestimate power of templatetags.
To show you how to use helpers, let’s translate Pylons AJAX tutorial to Django.
Continue Reading »
Posted in programming, django, helpers | Comments Off
May 2nd, 2007 by viestards
There are lots of tutorials in web concerning integrating AJAX into Django. But they look somehow out of place- you have to insert JavaScript test from view and it’s against DRY. So, I decided to implement JavaScript through templatetags.
First, you need to create templatetag (if you know nothing of templatetags, look at Django template documentation for Python developers.
For my example I will be using Prototype and Script.alo.us JavaScript frameworks
So first things first- let’s create templatetag:
def do_visual_effect(parser, token):
"adds various JS visual effects to chosen element"
effect = token.split_contents()
wigdet="new Effect."+effect[1]+'("'+ effect[2] +'")'
return VisualEffectNode(wigdet)
As you can see, visual effect function is very simple and now works only with two arguments- EffectName and div id , but we will fix that later.
Template Node class is the same as in Django documentation, so I will not get any deeper:
class VisualEffectNode(template.Node):
def __init__(self, widget_code):
print widget_code
self.widget_code= widget_code
def render(self, context):
return self.widget_code
Only thing left is to register it as templatetag, so at the bottom of file write down:
register.tag('visual_effect', do_visual_effect)
Now for the template part: I saved file as helpers.py, so I’m gonna insert
{% load helpers %}
at the top of template file.
Now when you need any effect, just call
<div id="my_old_div">
<b>sometext</b>
</div>
<a href=”#” onclick=’{% visual_effect SwitchOff my_old_div %}’>Get out of the way!</a>
This will create link, which remove div with id “my_old_div” when clicked.
But maybe we want to add some more arguments, such as duration? So, lets modify our code a little bit:
def do_visual_effect(parser, token):
"adds various JS visual effects to chosen element"
effect = token.split_contents()
# if addition opntions are ommited, add empty string
if len(effect)==3:
effect.append("")
wigdet="new Effect."+effect[1]+'("'+ effect[2] +'",{'+ effect[3] + '})'
return VisualEffectNode(wigdet)
I just appended dictionary-like structure where additional arguments can be inserted.
In template add something like this
<a href="#" onclick='{% visual_effect Highlight model2 duration:1 %}'>Highlight model1</a>
and we’re set!
That’s it for now, I try later to write something more.
Posted in programming, django, javascript, helpers | Comments Off
Nov 24th, 2006 by viestards
Some time ago I’have written simple tutorial how to create simple blog with Django in Latvian. You can find it here.
Posted in programming, django | Comments Off