| 1 | from django.conf import settings |
| 2 | from django.template import Library, Node, TemplateSyntaxError |
| 3 | from django.template.defaulttags import kwarg_re |
| 4 | |
| 5 | |
| 6 | register = Library() |
| 7 | |
| 8 | |
| 9 | class URLNode(Node): |
| 10 | |
| 11 | def __init__(self, view_name, args, kwargs, asvar): |
| 12 | self.view_name = view_name |
| 13 | self.args = args |
| 14 | self.kwargs = kwargs |
| 15 | self.asvar = asvar |
| 16 | |
| 17 | def render(self, context): |
| 18 | from django.core.urlresolvers import reverse, NoReverseMatch |
| 19 | args = [arg.resolve(context) for arg in self.args] |
| 20 | kwargs = dict([(smart_str(k, 'ascii'), v.resolve(context)) |
| 21 | for k, v in self.kwargs.items()]) |
| 22 | |
| 23 | view_name = self.view_name.resolve(context) |
| 24 | |
| 25 | # Try to look up the URL twice: once given the view name, and again |
| 26 | # relative to what we guess is the "main" app. If they both fail, |
| 27 | # re-raise the NoReverseMatch unless we're using the |
| 28 | # {% url ... as var %} construct in which cause return nothing. |
| 29 | url = '' |
| 30 | try: |
| 31 | url = reverse(view_name, args=args, kwargs=kwargs, |
| 32 | current_app=context.current_app) |
| 33 | except NoReverseMatch, e: |
| 34 | if settings.SETTINGS_MODULE: |
| 35 | project_name = settings.SETTINGS_MODULE.split('.')[0] |
| 36 | try: |
| 37 | url = reverse(project_name + '.' + view_name, |
| 38 | args=args, kwargs=kwargs, |
| 39 | current_app=context.current_app) |
| 40 | except NoReverseMatch: |
| 41 | if self.asvar is None: |
| 42 | # Re-raise the original exception, not the one with |
| 43 | # the path relative to the project. This makes a |
| 44 | # better error message. |
| 45 | raise e |
| 46 | else: |
| 47 | if self.asvar is None: |
| 48 | raise e |
| 49 | |
| 50 | if self.asvar: |
| 51 | context[self.asvar] = url |
| 52 | return '' |
| 53 | else: |
| 54 | return url |
| 55 | |
| 56 | |
| 57 | def url(parser, token): |
| 58 | """ |
| 59 | Returns an absolute URL matching given view with its parameters. |
| 60 | |
| 61 | This is a way to define links that aren't tied to a particular URL |
| 62 | configuration:: |
| 63 | |
| 64 | {% url "path.to.some_view" arg1 arg2 %} |
| 65 | |
| 66 | or |
| 67 | |
| 68 | {% url "path.to.some_view" name1=value1 name2=value2 %} |
| 69 | |
| 70 | The first argument is a path to a view. It can be an absolute python path |
| 71 | or just ``app_name.view_name`` without the project name if the view is |
| 72 | located inside the project. Other arguments are comma-separated values |
| 73 | that will be filled in place of positional and keyword arguments in the |
| 74 | URL. All arguments for the URL should be present. |
| 75 | |
| 76 | For example if you have a view ``app_name.client`` taking client's id and |
| 77 | the corresponding line in a URLconf looks like this:: |
| 78 | |
| 79 | ('^client/(\d+)/$', 'app_name.client') |
| 80 | |
| 81 | and this app's URLconf is included into the project's URLconf under some |
| 82 | path:: |
| 83 | |
| 84 | ('^clients/', include('project_name.app_name.urls')) |
| 85 | |
| 86 | then in a template you can create a link for a certain client like this:: |
| 87 | |
| 88 | {% url "app_name.client" client.id %} |
| 89 | |
| 90 | The URL will look like ``/clients/client/123/``. |
| 91 | """ |
| 92 | bits = token.split_contents() |
| 93 | if len(bits) < 2: |
| 94 | raise TemplateSyntaxError("'%s' takes at least one argument" |
| 95 | " (path to a view)" % bits[0]) |
| 96 | viewname = parser.compile_filter(bits[1]) |
| 97 | args = [] |
| 98 | kwargs = {} |
| 99 | asvar = None |
| 100 | bits = bits[2:] |
| 101 | if len(bits) >= 2 and bits[-2] == 'as': |
| 102 | asvar = bits[-1] |
| 103 | bits = bits[:-2] |
| 104 | |
| 105 | if len(bits): |
| 106 | for bit in bits: |
| 107 | match = kwarg_re.match(bit) |
| 108 | if not match: |
| 109 | raise TemplateSyntaxError("Malformed arguments to url tag") |
| 110 | name, value = match.groups() |
| 111 | if name: |
| 112 | kwargs[name] = parser.compile_filter(value) |
| 113 | else: |
| 114 | args.append(parser.compile_filter(value)) |
| 115 | |
| 116 | return URLNode(viewname, args, kwargs, asvar) |
| 117 | url = register.tag(url) |