Be careful will computing location on rendering. IPs will change in time. The 
pattern is not clear but expect the information to expire.

As you only extract the country, it shouldn't be an issue, but if you drill 
down to Zip code I would commend you to store it with the comment's author 
(less expensive) or the comment.

Brice Leroy 


On May 23, 2011, at 1:34 AM, bruno desthuilliers 
<bruno.desthuilli...@gmail.com> wrote:

> On May 23, 5:37 am, stevedegrace <degr...@gmail.com> wrote:
>> I developed a custom tag to look up the country of a certain IP
>> address using an IP to country database. It's sort of rough and ready,
>> but it should work. The idea is that you read the comment.ip_address,
>> feed it to the {% country %} tag, which then spits out the two letter
>> country code. The interesting thing is that the tag works once and
>> only once per page load if it has to render a variable. All subsequent
>> calls raise template.VariableDoesNotExist in the code below.
>> 
>> The interesting thing is that if you strip out the ability to
>> recognize an IP as a string and make it just render variables, and you
>> bind self.ip as a Variable object in the __init__ method and then try
>> to call its render method in the CountryNode's render method, it
>> actually raises AttributeError and claims that Variable object has no
>> attribute render. Weird! FYI, I'm using Django 1.2. Anyone have any
>> thoughts about what the heck is going on?
> 
> Avoid rebinding self.ip in the render method and it should work just
> fine.
> 
>> Here's the Python code:
> (snip imports)
>> class CountryNode(template.Node):
>>     def __init__(self, ip):
>>         self.ip = ip
>> 
>>     def render(self, context):
>>         if self.ip[0] in '\'"':
>>             self.ip = self.ip.strip('\'"')
> 
> don't rebind self.ip here.
> 
>>         else:
>>             try:
>>                 self.ip = Variable(self.ip).resolve(context)
> 
> don't rebind it here neither
> 
> You want to work on local variable, definitly.
> 
> HTH
> 
> -- 
> 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 
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/django-users?hl=en.
> 

-- 
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 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to