django template replace url by template var - django

Is there the possibility to dinamically replace a url call from a template?
in the form I insert:
{% include "mytemplate.html" with soggetto='gigi' %}
in the template
url: '{% url 'go_to_this_{{soggetto}}' %}'
is there a way to replace the {{soggetto}} part dinamically or should I add a parameter to the url?

You can use the add template filter:
{% url 'go_to_this_'|add:soggetto %}
However, it might be cleaner to include a soggetto parameter in the url, then you could do:
{% url 'go_to_this' soggetto %}

Related

Django SiteTree: menu renders but breadcrumbs doesn't when variable is in url

I am using django==1.11 and django-sitetree==1.9.0, all links without url variables render both on menu and breadcrumbs but urls with variables only render on menu(with correct links) and not on breadcrumbs.
Title: {{ object.title }}
URL: products:detail object.slug object.pk
URL as Pattern: Checked
url(r'^products/(?P<slug>[-\w\d]+)~(?P<pk>\d+)/$', views.ProductDetailView.as_view(), name='products:detail)
{% sitetree_breadcrumbs from "main-menu" template "sitetree/breadcrumbs_semantic.html" %}
Problem was with ~ sign in the url pattern, by changing the url pattern from
url(r'^products/(?P<slug>[-\w\d]+)~(?P<pk>\d+)/$', views.ProductDetailView.as_view(), name='products:detail)
to
url(r'^products/(?P<slug>[-\w\d]+)-(?P<pk>\d+)/$', views.ProductDetailView.as_view(), name='products:detail)
I could fix the problem.

Trouble creating static link using Django template system

I am attempting to create a static link to an html file that I will load into an <a> tag or an <iframe>. Here is how I create the url:
{% with '/static/projects/'|add:project.slug|add:'/'|add:f|add:'.html' as banner_static %}
{% static banner_static %}
{% endwith %}
This code creates the desired url (/static/projects/project-name/file-name.html), but as soon as I add this to an tag I get this:
test
Any ideas as to why it is being truncated? Am I not using the django template tag correctly?
My original plan did work, turned out I had not made sure the '{% static banner_static %}' was properly wrapped by the 'with' loop.

Django templates - can you pass arguments to template tags *args style?

I am trying to implement some breadcrumbs in my templates.
I some view urls take one argument and others two.
url(r'^firstpage/(?P<arg_one>(option1|option2))/(?P<unique_identidfier>[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890\-_]+)/', views.MyView.as_view(), name="two_arg_view"),
url(r'^secondpage/(?P<unique_identidfier>[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890\-_]+)/', views.MySecondView.as_view(), name="one_arg_view),
I would like to pass in an array of breadcrumbs to via get_context_data, then loop over them in the template:
<div class="breadcrumbs">
{%for crumb in breadcrumbs %}
<a href="{% url crumb.url arg1 optional_arg2 %}" >{{ crumb.text }}</a> >
{% endfor %}
{{ current_page }}
</div>
Is there a way I can pass an optional number of arguments to the url template tag?
In the same way you might when calling something with *args?
What I want to do (but doesn't work) is:
<a href="{% url crumb.url *crumb.args %}" >{{ crumb.text }}</a> >
An inclusion tag will most likely do what you're looking for.
Inclusion tags can accept the current template context and you can retrieve your view values from there instead of having to manually pass them in. By including a template that contains the div, anchor tag, etc, you keep your templates simple:
{% tag_url %}
instead of having to repeat a bunch of conditional statements and loops.
I wouldn't recommend doing this sort of logic in a view, as it's not encapsulated or re-usable.

NoReverseMatch Django Postman

Why does this return a NoReverseMatch?
From django-postman:
url(r'^write/(?:(?P<recipients>[\w.#+-:]+)/)?$', 'write', name='postman_write'),
Template:
<a href='{% url postman_write recipients=object.user %}'>Send Message</a>
This does not work either..
<a href='{% url postman_write object.user %}'>Send Message</a>
Returns: Reverse for 'postman_write' with arguments '(<User: admin>,)' and keyword arguments '{}' not found. What am I missing to construct this url properly? Thanks!
you are passing a user object in url tag. You need to pass the username.
{% url postman_write recipients=object.user.username %}
Also make sure you have included the postman urls in your main urls.py file.
Try:
{% url postman_write object.user.username %}

Django: construct value of {% include %} tag dynamically?

I'd like to use an {% include page.html %} tag in my Django template, and construct the value of page.html dynamically.
Is there any way to do this in Django?
Here's a pseudocode example:
{% include page_{{mode}}.html %}
Thanks!
From the docs:
The template name can either be a
variable or a hard-coded (quoted)
string, in either single or double
quotes.
So construct a variable in your view and pass it to your template via the context, say as 'modeTemplate': "page_"+mode+".html". Then do:
{% include modeTemplate %}
Assuming 'mode' is a python variable in your view code.
I think that goliney provided a better answer here
In summary:
You can combine django's built-in 'with' template tag to assign a dynamically constructed template name to a variable and the 'add' filter and to dynamically construct a template name.
For example:
{% with template_name='page_'|add:mode|add:".html" %}
{% include ""|add:template_name %}
{% endwith %}
You can concatenate plain strings and variables right in your templates using with tag and add filter,
see this SO answer.