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

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.

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 URL with {% URL %} from data string

I am trying to pass string information into a {% URL %}
I am getting the data from data like:
{% for p in proj %}
<tr>
<td>
{{ p.name }}
</td>
</tr>
{% endfor %}
The expected result would be something like:
Sample Info
Is this even possible?
Thanks.
From the documentation:
The first argument is a URL pattern name. It can be a quoted literal or any other context variable.
So you need to use {% url p.dir %}

django template url - append variable to string

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 %}

How do I get the first list objects value using a second list objects result in a django template

I have two object lists: firstobjectlist and secondobjectlist. With these two lists I want to get the values of the first object using the second object list result values.
For example:
{% for i in firstobjectlist %}
{% for value in secondobjectlist %}
<td align="left">{{i{{value.id}}}}</td>
{% endfor %}
{% endfor %}
When I excute the above code I get the error:
"Could not parse the remainder: '{{value.id' from 'i.{{value.id'"
Can anyone help show me how it should be done?
Firstly, django template variables have to have a space between the {{ and the contents.
You could use the with tag (presuming that the value of value.id is a key or index in firstobjectlist):
{% for i in firstobjectlist %}
{% for value in secondobjectlist %}
{% with value.id as j %}
<td align="left">{{ i.j }}</td>
{% endwith %}
{% endfor %}
{% endfor %}

How can I check the size of a collection within a Django template?

I have a list in my Django template. I want to do something only if the size of the list is greater than zero.
I have tried myList|length and myList|length_is but they have not been successful.
I've searched all over and don't see any examples. How can I check this?
See https://docs.djangoproject.com/en/stable/ref/templates/builtins/#if : just use, to reproduce their example:
{% if athlete_list %}
Number of athletes: {{ athlete_list|length }}
{% else %}
No athletes.
{% endif %}
If you're using a recent Django, changelist 9530 introduced an {% empty %} block, allowing you to write
{% for athlete in athlete_list %}
...
{% empty %}
No athletes
{% endfor %}
Useful when the something that you want to do involves special treatment for lists that might be empty.
A list is considered to be False if it has no elements, so you can do something like this:
{% if mylist %}
<p>I have a list!</p>
{% else %}
<p>I don't have a list!</p>
{% endif %}
If you tried myList|length and myList|length_is and its not getting desired results, then you should use myList.count
You can try with:
{% if theList.object_list.count > 0 %}
blah, blah...
{% else %}
blah, blah....
{% endif %}
This works:
{% if myList|length %}
Do something!
{% endif %}
Why there's so many answers here and why there's so much confusion is that this didn't always work. I think at one point template filters couldn't be used on arguments to the if statement and this was later added. It's also now possible to do things such as {% if myList|length >= 3 %}. The filter should do the equivalent of len(myList) so any type of object that can handle that would also be able to handle the |length filter.
Collection.count no bracket
{% if request.user.is_authenticated %}
{{wishlists.count}}
{% else %}0{% endif %}
I need the collection length to decide whether I should render table <thead></thead>
but don't know why #Django 2.1.7 the chosen answer will fail(empty) my forloop afterward.
I got to use {% if forloop.first %} {% endif %} to overcome:
<table>
{% for record in service_list %}
{% if forloop.first %}
<thead>
<tr>
<th>日期</th>
</tr>
</thead>
{% endif %}
<tbody>
<tr>
<td>{{ record.date }}</td>
</tr>
{% endfor %}
</tbody>
</table>