suggestion about {% url %}

11 views
Skip to first unread message

!张沈鹏(电子科大 08年毕业)

unread,
Aug 15, 2007, 10:15:25 PM8/15/07
to django-d...@googlegroups.com
I think the url tag if can work like below maybe more useful .

Example

mytags.py:
.....
@register.inclusion_tag('xxx.html')
def page_link(url_name):
return {"url_name":url_name}
______________________________________________________

xxx.html:

<a href="{% url url_name 1 %}">1</a>

______________________________________________________

Here , I modify the code of URLNode to do this :

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()])


def _reverse(url_name):
return reverse(url_name,args=args, kwargs=kwargs)

try:
return _reverse(self.view_name)
except NoReverseMatch:
try:
project_name = settings.SETTINGS_MODULE.split('.')[0]
return _reverse(project_name + '.' + self.view_name)
except NoReverseMatch:
url = context.get(self.view_name,'')
if url:
try:
return _reverse(url)
except NoReverseMatch:
try:
return _reverse(project_name + '.' + url)
except NoReverseMatch:
return ''
return ''

--
我的博客:http://zsp.javaeye.com/

-- 张沈鹏(ZhangShen Peng)

!张沈鹏(电子科大 08年毕业)

unread,
Aug 17, 2007, 9:22:45 PM8/17/07
to django-d...@googlegroups.com
Look at this ,

http://www.djangosnippets.org/snippets/373/

that why I think below code is useful

mytags.py:
.....
@register.inclusion_tag('xxx.html')
def page_link(url_name):
return {"url_name":url_name}
______________________________________________________

xxx.html:

<a href="{% url url_name 1 %}">1</a>

2007/8/16, !张沈鹏(电子科大 08年毕业) <zsp...@gmail.com>:

Malcolm Tredinnick

unread,
Aug 17, 2007, 11:55:18 PM8/17/07
to django-d...@googlegroups.com
On Sat, 2007-08-18 at 09:22 +0800, !张沈鹏(电子科大 08年毕业) wrote:
> Look at this ,
>
> http://www.djangosnippets.org/snippets/373/
>
> that why I think below code is useful
>
> mytags.py:
> .....
> @register.inclusion_tag('xxx.html')
> def page_link(url_name):
> return {"url_name":url_name}
> ______________________________________________________
>
> xxx.html:
>
> <a href="{% url url_name 1 %}">1</a>

If you're going to post things like this could you please explain what
the difference is? I've stared at this for more than a couple of minutes
both times you posted and I really have no idea what you are trying to
achieve. I'm not about to reverse-engineer dozens of lines of code in
your URLNode rewrite to work out what might be different from the
current stuff.

If you are trying to propose a new feature, explain what it does. And
how it remains backwards compatible with the existing code and why it
cannot be done with a third-party tag (which would mean why is it so
commonly used that we cannot live without it?). Otherwise you are not
helping yourself or helping us to help you.

Thanks,
Malcolm

--
For every action there is an equal and opposite criticism.
http://www.pointy-stick.com/blog/

!张沈鹏(电子科大 08年毕业)

unread,
Aug 18, 2007, 6:03:08 AM8/18/07
to django-d...@googlegroups.com
In a word , if {% url urlname %} could not find the urlname in url
patterns , it will try to find context[urlname] in patterns.


2007/8/18, Malcolm Tredinnick <mal...@pointy-stick.com>:

Malcolm Tredinnick

unread,
Aug 18, 2007, 6:13:04 AM8/18/07
to django-d...@googlegroups.com
On Sat, 2007-08-18 at 18:03 +0800, !张沈鹏(电子科大 08年毕业) wrote:
> In a word , if {% url urlname %} could not find the urlname in url
> patterns , it will try to find context[urlname] in patterns.

Aah ... so an attempt to allow variables in the URL tag. Okay. Now I
understand what you're trying to do.

This (variables in the URL tag) hasn't gathered a lot of support in the
past when it's been suggested -- there are always some supporters for
any idea, but it wasn't exactly critical mass. We got a bit stuck in
syntax discussions, too, if you look back at the old threads about this.
Transparently promoting something from a string to a variable is not
very uniform with the way template tags work (although I'll argue that
the syntax of the url tag isn't particularly uniform in the first
place).

Still, it's something to think about. You might get more feedback now
that your goal is clearer. Personally, I preferred the approach in
#3769, which was more explicit, but it didn't get enough support.

Regards,
Malcolm

--
Remember that you are unique. Just like everyone else.
http://www.pointy-stick.com/blog/

!张沈鹏(电子科大 08年毕业)

unread,
Aug 18, 2007, 7:28:23 AM8/18/07
to django-d...@googlegroups.com
Oh , sorry for not know the similar issue has been discuessed before.

Because my english very very poor :) , so I alway try to read and
write as little as I can .

Thanks for you pay attention to read my posting and reply it . You are
a good man .

I think passing a variable to url is useful when you are writing a
inclusion_tag.

for example a inclusion_tag:

{% page_link "bbs_posting_list" 25 221 %}

@register.inclusion_tag('xxx.html')
def page_link(url_name):
return {"url_name":url_name}


with a template like:
<a href="{% url urlname %}">xxx</a>

can generate links as below:

Prev 1 ... 23 24 25 26 27 ...221 Next

if variable can't work on with {%url%} , you have to do more work to
implement this .

2007/8/18, Malcolm Tredinnick <mal...@pointy-stick.com>:

Reply all
Reply to author
Forward
0 new messages