I ran into a problem this weekend, and I think I sort of understand
why it was happening, but I would like some experts to check my
reasoning :-).

I wanted to add some help_text to a form field that included a link to
another page of mine. I had something like this (from memory):

class RegisterForm(forms.Form):
   agree_tos = forms.BooleanField(required = True, label = 'I agree to
the Terms of Service',
      help_text = 'Please see our <a href="' + reverse("x.y.z",
kwargs={'policy':'tos'}) + '">TOS</a>')

This produced a nice help text link that worked, but unfortunately it
had the side effect of the django development server not being able to
find an unrelated view, but only sporadically. What would happen was I
would make a code change somewhere else, then visit a page, and I
would get some kind of django error about an unrelated view not being
found. I would shrug, and hit reload on the browser, and it would
work. Then it would come up again later. Reload, and it would work.

Eventually (!!!) I narrowed the problem down to that help_text line of
code, and I removed it. All was stable again.

I am new to Python, but now I am starting to have a hunch that because
that code is where it is, it may be executing before all of the URL
resolution magic has finished. I'm coming from the C++ world, and I
used to think of that code in the class body as a "kind of"
constructor, but really it is not. It runs when the class definition
is being processed, unrelated to object instance creation. Perhaps if
I had put that "agree_tos = ... " code inside an __init__ function for
the form, it would run when a form instance was being created, and by
then all the URL magic had been processed.

Run this python script to see what I mean:

class Test:
   print "Test"   # this is analogous to where I was calling reverse()

   def __init__(self):
      print "Test __init__"

print "hello"
x = Test()

The output is:

Test
hello
Test __init__

It was very revealing (to me) to see "Test" come out before hello. I
went back to some Python books and learned that this was the correct
expected behavior.

Is this related to the issue I was having? By running reverse() inside
the class like that, was that catching django in a not completely
initialized state? Would creating the agree_tos Boolean field
attribute in an __init__ function have worked? Thanks for any insights.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to