django template url - append variable to string - django

Im trying to add a variable to the end of a string and am getting issues with reverse match, from what I gather the below should work
{% for type in type_links %}
<li>{{ type.subnet_type }}</li>
{% endfor %}
I should have a url that has "all_1" for example in it.
however I am getting a reverse url error
Reverse for 'site_list' with arguments '('',)' not found. 1 pattern(s) tried: ['sites/site_list/(?P<display_filter>[\\w\\-]+)$']
is this correct way to add a variable to end the of the string?
EDIT:
url pattern, the pattern works as I have tested it manually before trying to create the URLs dyanmically
url(r'^site_list/(?P<display_filter>[\w\-]+)$', views.site_list, name='site_list'),
Thanks

You can do
{% for type in type_links %}
<li>
{% with type.id|stringformat:"s" as str_obj_id %}
{% with 'all_'|add:str_obj_id as extra_param %}
{{ type.subnet_type }}
{% endwith %}
{% endwith %}
</li>
{% endfor %}

Related

How to pass variable into Django block in url?

Context
I am trying to loop through a list of objects, then call on the name and id attribute and pass it into the Url block as regex to be captured in the urls.py later.
Problem
{%if people_found%}
{%for the_people in people_found%}
{{the_people.firstname}}
{%endfor%}
{%endif%}
Urls.py
app_name = 'Search4Bday'
urlpatterns= [path("search/profile/<str:firstname>/<int:id>", views.profile, name = "profile")]
Question
I'm not sure how I can pass the variables into the urls.py file if I can't call on the variable the_people with double curly braces. I'm also not sure if this is the correct way to pass in the variables into the regular expression capture portion of the urls.py. Any ideas?
Use add tag of Django Template Lang:
{% if people_found %}
{% for the_people in people_found %}
{{the_people.firstname}}
{% endfor %}
{% endif %}
edit: I didn't pay attention to urls.py and this will not work as wanted. Instead of adding slashes between variables put them directly. So this should work.
{% if people_found %}
{% for the_people in people_found %}
{{the_people.firstname}}
{% endfor %}
{% endif %}
I tried another method, I could have just done this:
{% if people_found %}
{% for the_people in people_found %}
{{the_people.firstname}}
{% endfor %}
{% endif %}
https://docs.djangoproject.com/en/4.0/ref/templates/language/

Django Templates: how flexible are variables?

I need to have a variable in a teplate that is basically a counter for a for-loop. The problem is: I need to manipulate it, depending on the for-element I am dealing with, I will have to reset the counter (an IF inside the for-loop).
Is this doable inside a Django template?
This is basically what I would like:
{% i = 0 %}
{% for l in list %}
{% if i == 5 %}
{% i = 0 %}
Do Something
<br>
{% else %}
{% i = i + 1 %}
{% endif %}
{% endfor %}
You can't with the builtin tags:
http://www.mail-archive.com/django-users#googlegroups.com/msg27399.html
The following snippets might be a good starting point:
Template counters
Access by index
EDIT: For the record, OP needed a conditional with divisibleby. See the accepted answer here plus the comments in this answer.
What you want is the forloop.counter variable that Django's template language provides.
https://docs.djangoproject.com/en/dev/ref/templates/builtins/#for
You would do something like:
{% for element in list %}
{% if forloop.counter > 5 %}
Do something
{% else %}
Do something else
{% endif %}
{% endfor %}
If you want to do something cyclically, you're basically doing a modulo operator (http://en.wikipedia.org/wiki/Modulo_operation), unfortunately, Django Template doesn't quite have this, but it does allow a 'divisible by' operator.
https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#divisibleby
So you'll add:
{% if {{ forloop.counter|divisibleby:"5" }} %}
{{ whatever }}
{% endif %}

How to affect a dict item to a variable in django template

i'm trying to affect an item list in template using {% with %}{% endwith %} but still get nothing in my varibale
this is the code
{% for item in data %}
<tr>
{% with value = item.user_id %}
{% endwith %}
<td><center>{{ item.Company_name }}</center></td>
i get this error
NoReverseMatch at /filter/
Reverse for '' with arguments '('',)' and keyword arguments '{}' not found.
You should put the view name in quotes:
{% url "accounts" value %}
Move the {%endwith%} after </td> tag as below.
{% for item in data %}
<tr>
{% with value = item.user_id %}
<td><a href="{% url accounts value %}"><center>{{ item.Company_name }}</center>
</a></td>
{% endwith %}
Scope of variable defined using with is only within the with block.

Am I using the django if template tag wrong?

I have this in my Django 1.2 template:
{% for m in model.related.iterator %}
{% if "str" or "Str" in m %}
{{m}}//I don't want anything here but put this in so I could see the output
{%else%}
{% if forloop.last %}
{% customtag "var" %}
{% endif %}
{% endif %}
{% endfor %}
I get the value of {{m}} no matter if it contains "str", "Str" or neither of them. Also, if I take out the or and compare one string it works but I would like to compare both without another else if. Any way to do that?
I believe what you want is
{% if "str" in m or "Str" in m %}
however, Glyn's answer would also work
try...
{% if "STR" in m|upper %}
Do something
{% endif %}

URL output in template is empty

URL pattern declaration from views.py
url(r'^intentions/(\d+)/$', 'intentions.views.show'),
When I am writing address directly, like intentions/1 works ok, but when I trying to display URL show for object like:
{% for i in intentions %}
{% url 'intentions.views.show' i.id as iUrl %}
<li>{{ i }}</li>
{% endfor %}
I am ending with empty href. Could someone give me any advice?
If you are using Django 1.3 or 1.4, make sure you are loading the new url tag. Add the following line to the top of your template:
{% load url from future %}
If you are using Django 1.4 or earlier, and you haven't loaded the new url tag as above, then you need to remove the single quotes from the url pattern:
{% url intentions.views.show i.id as iUrl %}
As an aside, it's recommended to name your url pattern:
url(r'^intentions/(\d+)/$', 'intentions.views.show', name='show_intention'),
Then change your template to:
{% for i in intentions %}
{% url 'show_intention' i.id as iUrl %}
<li>{{ i }}</li>
{% endfor %}
you could try
{% for i in intentions %}
<li><{{ i }}</li>
{% endfor %}
don't use {% url 'intentions.views.show' i.id as iUrl %}, it's maybe confused.
Also,you could try modifiy the url as follow
url(r'^intentions/(\d+)/$', 'intentions.views.show', name="intention_view"),
then,
{% for i in intentions %}
{% url "intention_view" i.id as iUrl %}
<li>{{ i }}</li>
{% endfor %}
If there are also ending empty href,try the first method.
'intentions.views.show'
Also give a name='' to your URL and use it in your template code.