Django

Code

Ticket #2556 (closed: fixed)

Opened 2 years ago

Last modified 2 years ago

simple_tag more capable than docs let on

Reported by: Karen Tracey <graybark@bellsouth.net> Assigned to: mtredinnick
Milestone: Component: Documentation
Version: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: 0 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

Opening per this email exchange on django-users:

From: Malcolm Tredinnick

On Thu, 2006-08-17 at 04:58 -0700, Karen Tracey wrote:
> I've been experimenting with writing some template tags by reading
> through the documentation and modifying some existing tags defined by
> admin.  I've got something like this:

> from django import template
> register = template.Library()

> def paginator_number(request, i, paginator, page_num):
>     #do stuff, eventually returning str output
> paginator_number = register.simple_tag(paginator_number)

> This works, and I don't understand why.  According to the documentation
> (http://www.djangoproject.com/documentation/templates_python/#shortcut...)
> a simple tag is supposed to only take one argument.  Mine, as you see,
> takes four.  But it works fine, and somehow the arguments are being
> checked, because if I pass in the wrong number I get a
> TemplateSyntaxError saying that "paginator_number takes 4 arguments".

It's a documentation bug, based on me not reading the code carefully
enough (I wrote those docs; my fault entirely). I'll fix it when I get a
chance. If you file a ticket and assign it to mtredinnick, that would
remind me.

[...]

> It's convenient not to have to check the arguments myself but I'm
> concerned about running into trouble down the line....

Now that I read the code more carefully, it inspects your function
(using the inspect.getargspec() function from the Python standard
library) and works out the positional arguments that it will take. They
are then all accounted for when it processes your tag, including
generating the error message syou are seeing.

What you are doing is completely future proof.

Regards,
Malcolm 

Thanks much for the quick answer!

Attachments

Change History

09/21/06 06:41:18 changed by mtredinnick

  • status changed from new to closed.
  • resolution set to fixed.

(In [3772]) Fixed #2556 -- Documented that simple_tag functions can take any number of arguments, not just one.


Add/Change #2556 (simple_tag more capable than docs let on)




Change Properties
Action