statik icon indicating copy to clipboard operation
statik copied to clipboard

How to access to project from a simple_tag?

Open PaulPichaureau opened this issue 7 years ago • 2 comments

I'm trying to code a link tag which creates a complete url link.

{% link "page" my-page %}

is supposed to create

page title

But I don't know how to access to viewsin the code of the tag:

@register.simple_tag(name='link')
def tag_link(*args):
    views = ???
    view_name, inst = args
    url =  add_url_path_component("/", views[view_name].reverse_url(inst) )
    (...)

PaulPichaureau avatar Feb 15 '18 08:02 PaulPichaureau

Would this do something that the url tag wouldn't? See the wiki here. The usage is a little different to what you're trying to achieve, however.

Usage (for example, when iterating through pages):

{% for page in pages %}
  <a href="{% url "pages-view-name", page.pk %}">{{ page.title }}</a>.
{% endfor %}

See the code for the extension here.

thanethomson avatar Feb 15 '18 09:02 thanethomson

Well, I was playing around with the code...

My idea is to have some sort of macro to cite quickly a page. Essentially, to obtain the result of

 <a href="{% url "pages-view-name", page.pk %}">{{ page.title }}</a>.

in one command.

PaulPichaureau avatar Feb 15 '18 21:02 PaulPichaureau