Opened 17 years ago

Closed 17 years ago

Last modified 17 years ago

#5990 closed (wontfix)

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

Reported by: johnfractal@… Owned by: nobody
Component: Core (Other) Version: dev
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: no UI/UX: no

Description

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

and

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()])
        try:
            return reverse(self.view_name, args=args, kwargs=kwargs)
        except NoReverseMatch:
            try:
                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@… 17 years ago.
patch to permalink()

Download all attachments as: .zip

Change History (6)

by johnfractal@…, 17 years ago

Attachment: permalink-patch.txt added

patch to permalink()

comment:1 by Chris Beaven, 17 years ago

Component: UncategorizedCore framework
Patch needs improvement: set
Triage Stage: UnreviewedDesign 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 by Malcolm Tredinnick, 17 years ago

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 by Chris Beaven, 17 years ago

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 by Malcolm Tredinnick, 17 years ago

Resolution: wontfix
Status: newclosed

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 by johnfractal@…, 17 years ago

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

Note: See TracTickets for help on using tickets.
Back to Top