Ticket #5408: 5408.patch

File 5408.patch, 6.3 KB (added by Philippe Raoult, 17 years ago)

changed url to accept vars and quoted strings for the first argument (view path)

  • django/template/defaulttags.py

     
    331331        return self.mapping.get(self.tagtype, '')
    332332
    333333class URLNode(Node):
    334     def __init__(self, view_name, args, kwargs):
    335         self.view_name = view_name
     334    def __init__(self, view_var_or_name, args, kwargs):
     335        self.view_var_or_name = view_var_or_name
    336336        self.args = args
    337337        self.kwargs = kwargs
    338338
     
    340340        from django.core.urlresolvers import reverse, NoReverseMatch
    341341        args = [arg.resolve(context) for arg in self.args]
    342342        kwargs = dict([(smart_str(k,'ascii'), v.resolve(context)) for k, v in self.kwargs.items()])
     343        view_name = resolve_variable(self.view_var_or_name, context)
    343344        try:
    344             return reverse(self.view_name, args=args, kwargs=kwargs)
     345            return reverse(view_name, args=args, kwargs=kwargs)
    345346        except NoReverseMatch:
    346347            try:
    347348                project_name = settings.SETTINGS_MODULE.split('.')[0]
    348                 return reverse(project_name + '.' + self.view_name, args=args, kwargs=kwargs)
     349                return reverse(project_name + '.' + view_name, args=args, kwargs=kwargs)
    349350            except NoReverseMatch:
    350351                return ''
    351352
     
    954955
    955956    This is a way to define links that aren't tied to a particular URL configuration::
    956957
    957         {% url path.to.some_view arg1,arg2,name1=value1 %}
     958        {% url "path.to.some_view" arg1,arg2,name1=value1 %}
     959        {% url path_variable arg1,arg2,name1=value1 %}
    958960
    959961    The first argument is a path to a view. It can be an absolute python path
    960962    or just ``app_name.view_name`` without the project name if the view is
  • tests/regressiontests/templates/tests.py

     
    783783
    784784            ### URL TAG ########################################################
    785785            # Successes
    786             'url01' : ('{% url regressiontests.templates.views.client client.id %}', {'client': {'id': 1}}, '/url_tag/client/1/'),
    787             'url02' : ('{% url regressiontests.templates.views.client_action client.id, action="update" %}', {'client': {'id': 1}}, '/url_tag/client/1/update/'),
    788             'url03' : ('{% url regressiontests.templates.views.index %}', {}, '/url_tag/'),
    789             'url04' : ('{% url named.client client.id %}', {'client': {'id': 1}}, '/url_tag/named-client/1/'),
    790             'url05' : (u'{% url метка_оператора v %}', {'v': u'Ω'},
     786            'url01' : ('{% url "regressiontests.templates.views.client" client.id %}', {'client': {'id': 1}}, '/url_tag/client/1/'),
     787            'url02' : ('{% url "regressiontests.templates.views.client_action" client.id, action="update" %}', {'client': {'id': 1}}, '/url_tag/client/1/update/'),
     788            'url03' : ('{% url "regressiontests.templates.views.index" %}', {}, '/url_tag/'),
     789            'url04' : ('{% url "named.client" client.id %}', {'client': {'id': 1}}, '/url_tag/named-client/1/'),
     790            'url05' : (u'{% url "метка_оператора" v %}', {'v': u'Ω'},
    791791                    '/url_tag/%D0%AE%D0%BD%D0%B8%D0%BA%D0%BE%D0%B4/%CE%A9/'),
     792            'url06' : ('{% url view_path client.id %}', {'view_path': "regressiontests.templates.views.client", 'client': {'id': 1}}, '/url_tag/client/1/'),
     793            'url07' : ('{% url view_path client.id, action="update" %}', {'view_path': "regressiontests.templates.views.client_action", 'client': {'id': 1}}, '/url_tag/client/1/update/'),
     794            'url08' : ('{% url view_path %}', {'view_path': "regressiontests.templates.views.index", }, '/url_tag/'),
     795            'url09' : ('{% url view_path client.id %}', {'view_path': "named.client", 'client': {'id': 1}}, '/url_tag/named-client/1/'),
     796            'url10' : (u'{% url view_path v %}', {'view_path': "метка_оператора", 'v': u'Ω'},
     797                    '/url_tag/%D0%AE%D0%BD%D0%B8%D0%BA%D0%BE%D0%B4/%CE%A9/'),
    792798
    793799            # Failures
    794800            'url-fail01' : ('{% url %}', {}, template.TemplateSyntaxError),
    795             'url-fail02' : ('{% url no_such_view %}', {}, ''),
    796             'url-fail03' : ('{% url regressiontests.templates.views.client no_such_param="value" %}', {}, ''),
     801            'url-fail02' : ('{% url "no_such_view" %}', {}, ''),
     802            'url-fail03' : ('{% url "regressiontests.templates.views.client" no_such_param="value" %}', {}, ''),
     803            'url-fail04' : ('{% url view_path %}', {'view_path': "no_such_view"}, ''),
     804            'url-fail05' : ('{% url view_path no_such_param="value" %}', {'view_path': "regressiontests.templates.views.client"}, ''),
    797805        }
    798806
    799807        # Register our custom template loader.
  • docs/templates.txt

     
    924924view function and optional parameters. This is a way to output links without
    925925violating the DRY principle by having to hard-code URLs in your templates::
    926926
    927     {% url path.to.some_view arg1,arg2,name1=value1 %}
     927    {% url "path.to.some_view" arg1,arg2,name1=value1 %}
     928    {% url path_in_a_var arg1,arg2,name1=value1 %}
    928929
    929930The first argument is a path to a view function in the format
    930 ``package.package.module.function``. Additional arguments are optional and
     931``package.package.module.function`` (either as a string literal with quotes or
     932the name of a variable containing the path). Additional arguments are optional and
    931933should be comma-separated values that will be used as positional and keyword
    932934arguments in the URL. All arguments required by the URLconf should be present.
    933935
     
    950952
    951953**New in development version:** If you're using `named URL patterns`_,
    952954you can refer to the name of the pattern in the ``url`` tag instead of
    953 using the path to the view.
     955using the path to the view. The fact that you can use a variable for the
     956path is new. Previous versions used the path directly but without the quotes,
     957which means any template written using this convention will break.
    954958
    955959.. _named URL patterns: ../url_dispatch/#naming-url-patterns
    956960
Back to Top