#3291: get_absolute_url cannot be encanced with optional parameters, because it
gets curried for settings.ABSOLUTE_URL_OVERRIDES (used for {% link_to ...
%} implementation)
-------------------------------------------------------+--------------------
Reporter: David Danier <[EMAIL PROTECTED]> | Owner: adrian
Type: enhancement | Status: new
Priority: normal | Milestone:
Component: Core framework | Version: SVN
Severity: major | Keywords:
-------------------------------------------------------+--------------------
If you want to have (optional) extra parameters for get_absolute_url(),
that's not possible because this method gets curried (because of
ABSOLUTE_URL_OVERRIDES). But optional extra-parameters could be an
enhancement and a simple way of creating URLs using parameters (for
example some URL to edit the object).
I wanted to use this, because I tried to implement an link_to-template tag
that can get parameters to add extra functionality to the simple
get_absolute_url()-method. This would allow a simple way to get links for
object-actions like edit/delete/move/...
It can be fixed by allowing *args and **kwargs in
django.db.models.base.get_absolute_url:
{{{
def get_absolute_url(opts, func, self, *args, **kwargs):
return settings.ABSOLUTE_URL_OVERRIDES.get('%s.%s' % (opts.app_label,
opts.module_name), func)(self, *args, **kwargs)
}}}
If someone is interested...the template-tag-code:
{{{
from django import template
from django.template import loader
class LinkToNode(template.Node):
def __init__(self, context_var_name, kwargs):
self.context_var_name = context_var_name
self.kwargs = kwargs
def render(self, context):
print self.kwargs
try:
obj =
template.resolve_variable(self.context_var_name, context)
except template.VariableDoesNotExist:
return ''
try:
return obj.get_absolute_url(**self.kwargs)
except:
return ''
def link_to(parser, token):
tokens = token.contents.split()
# Now tokens is a list like this:
# ['link_to', 'article', 'foo=bar']
# ['link_to', 'article', 'foo=bar', 'bla=fasl']
if len(tokens) < 1:
raise template.TemplateSyntaxError, "%r tag requires more
than one argument" % tokens[0]
context_var_name = tokens[1]
kwargs = dict([tuple(part.split('=')) for part in tokens[1:] if
'=' in part])
return LinkToNode(context_var_name, kwargs)
}}}
Template example:
{{{
{% link_to object action=edit %}
}}}
Perhaps there is some really good reason not to do it this way...?
(The args/kwargs that are passed to get_absolute_url() could be filtered
like done in the dispatcher I think)
--
Ticket URL: <http://code.djangoproject.com/ticket/3291>
Django <http://code.djangoproject.org/>
The web framework for perfectionists with deadlines.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---