Opened 12 years ago

Closed 5 years ago

#2615 closed New feature (duplicate)

[patch] Make reversed url-resolving aware of matched kwargs and extra options

Reported by: norjee@… Owned by: nobody
Component: Core (Other) Version:
Severity: Normal Keywords:
Cc: Triage Stage: Someday/Maybe
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


The current reversed url-resolving only returned a matching url. This is fine for simple issues, but often more info is needed.

For example:

  • It is usefull to know which of the supplied kwargs have been matched, so one could, for example, supply the other ones in the query string.
  • An url pattern can have default-arguments. These could be used to create a middleware that redirects to https if, for example, the default argument https:True has been supplied. It would be usefull to build a secure url with reversed url-resolving, but for that, the default arguments need to be known.

This patch introduced a new method, reverse_match (the old reverse method still returns the url, but uses reverse_match to find it), which returns a match-object:

class ReversedMatch:
    url is te resulting url
    matched_args is a list with matched named args
    default_args a hash, with the associated default args
    def __init__(self, url , matched_kwargs , default_args):
        self.url = url
        self.matched_kwargs = matched_kwargs
        self.default_args = default_args
    def __add__(self , other):
        new_default_args = self.default_args.copy() 
        return ReversedMatch(self.url + other.url , self.matched_kwargs + other.matched_kwargs , new_default_args)
    def get_unmatched_kwargs(self, kwargs):
        return dict([(k, v) for k , v in kwargs.items() if not k in self.matched_kwargs])
    def get_default_args(self):
        return self.default_args
    def get_url(self):
        return '/' + self.url

As an example, the follwing method appends builds an url through this new reversed url-lookup, and then appends the unmatched kwargs:

def find_url_for_view(view , *args , **kwargs):
     # url = reverse(view, *args , **kwargs )
     # logger.debug(args)
     # logger.debug(kwargs)
     match = reverse_match(view,  args = args ,  kwargs = kwargs)
     url = match.get_url()
     # logger.debug(re.split(r"[/.]" , url))
     # now i want to append a query string
     # i want to use the unmatched kwargs for this
     query_string = "&".join(["=".join([k, str(v)]) for k , v in match.get_unmatched_kwargs(kwargs).items()])
     if len(query_string) > 0:
         query_string = "?" + query_string
     return url + query_string

Attachments (2)

url_resolve.diff (5.7 KB) - added by norjee@… 12 years ago.
Patch for (3.4 KB) - added by norjee@… 12 years ago.
url_for template tag. More as an example than to be fully useful (it has my own debug code in it)

Download all attachments as: .zip

Change History (8)

Changed 12 years ago by norjee@…

Attachment: url_resolve.diff added

Patch for

Changed 12 years ago by norjee@…

Attachment: added

url_for template tag. More as an example than to be fully useful (it has my own debug code in it)

comment:1 Changed 11 years ago by Chris Beaven

Triage Stage: UnreviewedDesign decision needed

comment:2 Changed 10 years ago by Jacob

Triage Stage: Design decision neededSomeday/Maybe

comment:3 Changed 7 years ago by Łukasz Rekucki

Severity: normalNormal
Type: enhancementNew feature

comment:4 Changed 6 years ago by Aymeric Augustin

UI/UX: unset

Change UI/UX from NULL to False.

comment:5 Changed 6 years ago by Aymeric Augustin

Easy pickings: unset

Change Easy pickings from NULL to False.

comment:6 Changed 5 years ago by Aymeric Augustin

Resolution: duplicate
Status: newclosed

Duplicate of #16391 which contains more information.

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