Django URL with {% URL %} from data string - django

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

Related

get a List of List object and how to handle it in html with django

index.html
{% block content %}
{{playerList}}
{% for player in playerList %}
{{player.value.indexOf(0)}}
{% empty %}
<tr>
<td class="bg-light text-center font-italic" colspan="3">You haven't registered any players yet.</td>
</tr>
{% endfor %}
{% endblock %}
playerList is a list of lists, which is returned from views.py file.
i.e playerList=[["chamo",'1'],["gir",'2'],["frt",'2']]
if I want to get the "chamo" from playerList, how should I write it in html file?
You can just do {{ player.0 }}.

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.

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

Why url() give me no values when calling inside included template?

Here is the code:
#list.html
{% if intentions %}
<table class="table">
{% for i in intentions %}
{% url 'intentions.views.show' i.id as iUrl %}
<tr>
<td width="20%">
<span class="label label-success">x prayers</span>
</td>
<td><h3>{{ i }}</h3></td>
</tr>
{% endfor %}
</table>
{% else %}
<p>
No intentions are available.
</p>
{% endif %}
If this code is inside index.html everything is ok. If I try to move this code to separate file list.html and include in index.html like:
{% include "list.html" with intentions=entries %}
I have no values in url attribute of <a> tag. Could someone give me answer why?
Your list.html template might be missing
{% load url from future %}
Because you are using the 'as' syntax, the url tag fails silently.

How to use if/else condition on Django Templates?

I have the following dictionary passed to a render function, with sources being a list of strings and title being a string potentially equal to one of the strings in sources:
{'title':title, 'sources':sources})
In the HTML template I'd like to accomplish something among the lines of the following:
{% for source in sources %}
<tr>
<td>{{ source }}</td>
<td>
{% if title == {{ source }} %}
Just now!
{% endif %}
</td>
</tr>
{% endfor %}
However, the following block of text results in an error:
TemplateSyntaxError at /admin/start/
Could not parse the remainder: '{{' from '{{'
...with {% if title == {{ source }} %} being highlighted in red.
You shouldn't use the double-bracket {{ }} syntax within if or ifequal statements, you can simply access the variable there like you would in normal python:
{% if title == source %}
...
{% endif %}
Sorry for comment in an old post but if you want to use an else if statement this will help you
{% if title == source %}
Do This
{% elif title == value %}
Do This
{% else %}
Do This
{% endif %}
For more info see https://docs.djangoproject.com/en/3.2/ref/templates/builtins/#if
{% for source in sources %}
<tr>
<td>{{ source }}</td>
<td>
{% ifequal title source %}
Just now!
{% endifequal %}
</td>
</tr>
{% endfor %}
or
{% for source in sources %}
<tr>
<td>{{ source }}</td>
<td>
{% if title == source %}
Just now!
{% endif %}
</td>
</tr>
{% endfor %}
See Django Doc
You try this.
I have already tried it in my django template.
It will work fine. Just remove the curly braces pair {{ and }} from {{source}}.
I have also added <table> tag and that's it.
After modification your code will look something like below.
{% for source in sources %}
<table>
<tr>
<td>{{ source }}</td>
<td>
{% if title == source %}
Just now!
{% endif %}
</td>
</tr>
</table>
{% endfor %}
My dictionary looks like below,
{'title':"Rishikesh", 'sources':["Hemkesh", "Malinikesh", "Rishikesh", "Sandeep", "Darshan", "Veeru", "Shwetabh"]}
and OUTPUT looked like below once my template got rendered.
Hemkesh
Malinikesh
Rishikesh Just now!
Sandeep
Darshan
Veeru
Shwetabh