Django

Code

Ticket #3291 (closed: fixed)

Opened 2 years ago

Last modified 1 year ago

[patch] get_absolute_url cannot be encanced with optional parameters, because it gets curried for settings.ABSOLUTE_URL_OVERRIDES (used for {% link_to ... %} implementation)

Reported by: David Danier <goliath.mailinglist@gmx.de> Assigned to: nobody
Milestone: Component: Core framework
Version: SVN Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

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)

Attachments

ticket_3291__revision_6467.diff (0.6 kB) - added by __hawkeye__ on 10/08/07 17:17:15.
Adds ability to pass arguments to get_absolute_url method which are passed through to underlying function

Change History

01/12/07 13:41:39 changed by David Danier <goliath.mailinglist@gmx.de>

  • priority changed from normal to lowest.
  • severity changed from major to trivial.

Done this using a second method to get the url now, so I lower the priority/severity. But perhaps someone could look after this anyway. (second method has one advantage btw: you can use the result of get_absolute_url() there, which makes thing easier)

01/20/07 17:46:10 changed by mir@noris.de

  • stage changed from Unreviewed to Design decision needed.

09/16/07 10:56:52 changed by ubernostrum

#2140 is a different symptom of the same problem.

10/08/07 17:17:15 changed by __hawkeye__

  • attachment ticket_3291__revision_6467.diff added.

Adds ability to pass arguments to get_absolute_url method which are passed through to underlying function

10/08/07 17:21:50 changed by __hawkeye__

  • has_patch set to 1.
  • summary changed from get_absolute_url cannot be encanced with optional parameters, because it gets curried for settings.ABSOLUTE_URL_OVERRIDES (used for {% link_to ... %} implementation) to [patch] get_absolute_url cannot be encanced with optional parameters, because it gets curried for settings.ABSOLUTE_URL_OVERRIDES (used for {% link_to ... %} implementation).

It is not currently possible to modify the method signature of get_absolute_url.

We use this simple patch internally and provide defaults for any arguments so that get_absolute_url behaves as expected, but allows for multiple URL schemes per object.

Note: The provided patch does not include the template tag code.

10/26/07 17:36:15 changed by SmileyChris

  • stage changed from Design decision needed to Ready for checkin.

I can't see any downsides to adding this functionality.

11/29/07 10:32:09 changed by mtredinnick

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

(In [6732]) Fixed #3291 -- Allow calling get_absolute_url() with extra positional and keyword arguments. Not usually required, but useful for people wanting to write some kinds of customisations. Patch from hawkeye.


Add/Change #3291 ([patch] get_absolute_url cannot be encanced with optional parameters, because it gets curried for settings.ABSOLUTE_URL_OVERRIDES (used for {% link_to ... %} implementation))




Change Properties
Action