I have dict with hyderlink, example :
data = [{a:<\a href="http://someexample.com/a">a</a>,
b:'<\a href="http://someexample.com/b">b</a>'}]
Note : here i have add /a href because stack overflow takes it has hyperlink
if i want to output this in html , it displays a normal html text instead of hyperlink
template
<table>
{% for fetch in data %}
<tr>
<td>{{ fetch.a }}</td>
<td>{{ fetch.b }}</td>
</tr>
{% endfor %}
</table>
it gives output like html text instead of hyperlink
<\a href="http://someexample.com/a">a
<\a href="http://someexample.com/b">b
any help it really appreciate it.
Instead of storing the entire anchor tag, you should be storing just the URL (using URLField if you're storing it in a model), and then include it in your template as follows:
<table>
{% for fetch in data %}
<tr>
<td>{{ fetch.a }}</td>
<td>{{ fetch.b }}</td>
</tr>
{% endfor %}
</table>
This is happening because of automatic string escaping in the template engine. You can prevent escaping with the safe filter, like:
<table>
{% for fetch in data %}
<tr>
<td>{{ fetch.a|safe }}</td>
<td>{{ fetch.b|safe }}</td>
</tr>
{% endfor %}
</table>
Related
views.html
{% for products in product %}
<tr>
<td>{{ products.quantity_deliver1 }}</td>
<td>{{ products.quantity_deliver2 }}</td>
<td>{{ Code Here }}</td>
</tr>
{% empty %}
<tr>
<td colspan="3" class="text-center bg-warning">No Products</td>
</tr>
{% endfor %}
How do I add products.quantity_deliver1 + products.quantity_deliver2 and output the sum in the 3rd data cell.
You can use builtin django template tag add for adding two.
{{products.quantity_deliver1|add:products.quantity_deliver2}}
Ref - https://docs.djangoproject.com/en/dev/ref/templates/builtins/#add
i have a django project that is connected to SQL Server i tried to retrieve data from the database.
if i try to display all the data it run correct and display all the values.
but if i try to display the data in an html table it doesn't display anything.
views.py
def connect(request):
conn = pyodbc.connect(
'Driver={ODBC Driver 17 for SQL Server};'
'Server=DESKTOP-LPD1575\\SQLEXPRESS;'
'Database=testDB;'
'UID=test;'
'PWD=test;'
)
cursor = conn.cursor()
c = cursor.execute('SELECT * FROM Artist')
return render (request,'connect.html',{"c":c})
connect.html
{% for row in c %}
{{ row }}
{% endfor %}
this code in the html template work and display the data.
but if i try to do as below it will not work
<table align = "center">
<tr align="center">
<th>ID</th>
<th>FolderNumber</th>
<th>Title</th>
</tr>
{% for row in c %}
<tr align="center">
<td>{{ row.id }}</td>
<td>{{ row.artistName }}</td>
<td>{{ row.activeFrom }}</td>
</tr>
{% endfor %}
</table>
anyone can help me?
Each row in your result is a list, not a dict. You would need to use indexes not keys:
{% for row in c %}
<tr align="center">
<td>{{ row.0 }}</td>
<td>{{ row.1 }}</td>
<td>{{ row.2 }}</td>
</tr>
{% endfor %}
or better
{% for row in c %}
<tr align="center">
{% for item in row %}
<td>{{ item }}</td>
{% endfor %}
</tr>
{% endfor %}
However, really you should not be running SQL queries directly, but define a model for your table and access it that way.
<div class="person_roles_div">
<table>
<thead></thead>
<tbody>
{% for position in person.positions %}
<tr>
<td>{{ position.provider|default:"" }} {{ position.role }}</td>
<td>{{ position.facility|default:"" }}</td>
<td>{{ position.startdate|default:"" }}</td>
<td>{{ position.enddate|default:"" }}</td>
<td>
<button id="remove_position_sk_{{ position.position_sk }}" class="position_remove_button">Remove</button>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
In the first four td tags that output a context variable and specify a default, PyCharm is highlighting the : and blank space right before and after each ""
The hovertip message is (( or {% expected. The page runs fine.
Any idea what setting I should looking at?
it's "Python template languages". See the screenshot:
Make sure it's set to "Django"
I am kinda stuck.
Here is the situation. I have 2 for loops. One that loops through categories and the other one that loops through a match set. Here is the problem:
I need to get all the matches from that category...
Here is what I have:
{% for category in tournament.get_categories %}
<div class="line" style="margin-top: 25px; margin-bottom: 40px;"></div>
<div class="container">
<h3 class="margin-reset" id="{{ category.slug }}">{% trans "Matches schedule" %}<span> | {{ category.name }}</span></h3>
<table class="standard-table table">
<thead>
<tr>
<th>Match</th>
<th>Heure</th>
<th>Plateau</th>
<th>Équipe bleu</th>
<th>Équipe gris</th>
<th>Équipe noir</th>
<th>Résultat</th>
</tr>
</thead>
<tbody>
{% for match in tournament.match_set.filter(category=category) %}
<tr>
<td>{{ match.match_num }}</td>
<td>{{ match.time }}</td>
<td>{{ match.plateau }}</td>
<td>{{ match.blue_team.name }}</td>
<td>{{ match.grey_team.name }}</td>
<td>{{ match.black_team.name }}</td>
<td>{{ match.get_url_string }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endfor %}
As you guys probably guessed it, I get this error:
Could not parse the remainder: '(category=category)' from 'tournament.match_set.filter(category=category)'
What can I do?
Thanks,
Ara
EDIT:
Here is the solution:
The custom tag:
#register.filter
def get_matches_by_category(value, category):
return value.match_set.filter(category=category)
And the use:
{{ tournament|get_matches_by_category:category }}
I created a custom templatetag and used that filter.
It's kinda an overkill but...oh well.
What I have done in a very similar case is pass the list of categories to the template, adding them a new attribute with the matches, like:
for category in categories:
category.matches = Match.objects.filter(tournament=tournament, category=category)
It's kinda slow, but you can work it and reduce the number of queries
I would pass the categories list to the template after this
Imagine a following use case: I have a dictionary of report templates. Each report template has an ID and a name. It looks more or less like this:
reports = [ { 'ID' : 'A1',
'NAME' : 'special report 1 for department A',
... (other fields irrelevant to the question's matter)
},
{ 'ID' : 'X9',
'NAME' : 'special report 9 for department X',
... (other fields irrelevant to the question's matter)
},
]
Access to and visibility of the reports are to be stored in a database.
I created a form with field names equal to reports' IDs. And everything goes fine until the moment I have to show the form in the template. I don't know how to access the proper form's field. This syntax doesn't work:
{% for report in reports %}
<tr>
<td>{{ report.ID }}</td>
<td>{{ report.NAME }}</td>
<td>{{ form.report.ID }}</td>
</tr>
{% endfor %}
Also:
{% for report in reports %}
<tr>
<td>{{ report.ID }}</td>
<td>{{ report.NAME }}</td>
{% with report.ID as key %}
<td>{{ form.key }}</td>
{% endwith %}
</tr>
{% endfor %}
doesn't work.
How should the syntax look like? What should be placed instead of the question marks in the code below?
{% for report in reports %}
<tr>
<td>{{ report.ID }}</td>
<td>{{ report.NAME }}</td>
<td>{{ form.??? }}</td>
</tr>
{% endfor %}
I crossed upon this solution: Django Templates: Form field name as variable? and I guess I'll have to use it if there won't be any other way to solve my problem.
It's discouraged because the templates are supposed to be as without logic as possible. But, in practice, this comes up all the time. You can make a template filter that does this:
#register.filter(name='get')
def get(o, index):
try:
return o[index]
except:
return settings.TEMPLATE_STRING_IF_INVALID
Then, in the template, after you load the templatetags library appropriately:
{% for report in reports %}
<tr>
...
<td>{{ form|get:report.ID }}</td>
</tr>
{% endfor %}
Try this:
{{ form.fields.key }}