Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#5990 closed (wontfix)

permalink() works a little different from {% url %}

Reported by: johnfractal@… Owned by: nobody
Component: Core (Other) Version: master
Severity: Keywords: permalink url
Cc: Triage Stage: Design decision needed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: UI/UX:


I'v wasted one whole night with this problem, and I got the "bug", just here:

def permalink(func):
    Decorator that calls urlresolvers.reverse() to return a URL using
    parameters returned by the decorated function "func".

    "func" should be a function that returns a tuple in one of the
    following formats:
        (viewname, viewargs)
        (viewname, viewargs, viewkwargs)
    from django.core.urlresolvers import reverse
    def inner(*args, **kwargs):
        bits = func(*args, **kwargs)
        return reverse(bits[0], None, *bits[1:3])
    return inner


class URLNode(Node):
    def __init__(self, view_name, args, kwargs):
        self.view_name = view_name
        self.args = args
        self.kwargs = kwargs

    def render(self, context):
        from django.core.urlresolvers import reverse, NoReverseMatch
        args = [arg.resolve(context) for arg in self.args]
        kwargs = dict([(smart_str(k,'ascii'), v.resolve(context))
                       for k, v in self.kwargs.items()])
            return reverse(self.view_name, args=args, kwargs=kwargs)
        except NoReverseMatch:
                project_name = settings.SETTINGS_MODULE.split('.')[0]
                return reverse(project_name + '.' + self.view_name,
                               args=args, kwargs=kwargs)
            except NoReverseMatch:
                return ''

The implement of {% url %} is more convenient to use, and I hope permalink() would be as convenient as {% url %}.

What's your opinion?

Attachments (1)

permalink-patch.txt (1.1 KB) - added by johnfractal@… 6 years ago.
patch to permalink()

Download all attachments as: .zip

Change History (6)

Changed 6 years ago by johnfractal@…

patch to permalink()

comment:1 Changed 6 years ago by SmileyChris

  • Component changed from Uncategorized to Core framework
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement set
  • Triage Stage changed from Unreviewed to Design decision needed

If we were to do this (and I'm not sure we should), then the patch isn't good enough: permalink may be passed a view rather than just a string containing the view name, so you'd have to check for that before just adding a string to it.

comment:2 Changed 6 years ago by mtredinnick

Can somebody explain in simple words what the intention is here so that we can evaluate the patch against what it's meant to be doing? I'm not sure I see what the problem is with permalink().

comment:3 Changed 6 years ago by SmileyChris

It seems that what john is suggesting is that the permalink should try adding the project name in front of the view name, in case it wasn't included (since that's all the {% url %} tag does differently)

comment:4 Changed 6 years ago by mtredinnick

  • Resolution set to wontfix
  • Status changed from new to closed

In that case, no, this isn't a good idea. In fact, it's not even a good idea for the url template tag, but that damage has already been. permalink() is a decorator for function objects primarily. You should just be passing it the right function in the first place.

comment:5 Changed 6 years ago by johnfractal@…

well, by using the url() in, now I needn't to use that patch, :-)

Add Comment

Modify Ticket

Change Properties
<Author field>
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'

E-mail address and user name can be saved in the Preferences.

Note: See TracTickets for help on using tickets.