On Fri, Apr 9, 2010 at 1:41 PM, Ken Lacey <ken.la...@dit.ie> wrote: > Hi > > I am trying to check an IP so some information will/will not be > displayed based on the first 8 digites of the remote IP address. > > I have a vairable ip_address which contains the remote address and a > vairable accept_ip with a value "192.168." but when I carry out the > following it is not working correctly. > > {% ifequal ip_address|slice:"8" accept_ip %} > do something > {% endifequal %} > > > I have also tried > {% ifequal ip_address|slice:"8" "192.168." %} > do something > {% endifequal %} > > and > {% ifequal ip_address|slice:"8"|stringformat:"s" "192.168." %} > do something > {% endifequal %} > > the same results appaear whether the ip_address is 192.168..... or any > other range. > > Thanks > > Ken >
You shouldn't do logic like this in the template - as you can see, it is quite hard! The simplest option is to calculate whether that IP is in the correct range in the view and set a variable in the context to indicate that. If you are iterating through a list of things, you would probably want to write a templatetag to do the comparison for you. PS: Treating IP addresses as strings, and performing string manipulation to test belonging is poor form. I would recommend the netaddr library, and write code like this: >>> import netaddr >>> range = netaddr.IPNetwork('192.168.0.0/16') >>> addr = netaddr.IPAddress('192.168.12.4') >>> addr in range True Cheers Tom -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@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.