using {{ request.get_host }} in template - django

Template file that is located in one of the apps:
{% extends 'base_well_surfer.html' %}
{% block content %}
<table class="table table-bordered table-hover">
<thead class="thead-dark">
<tr>
<th>Options</th>
</tr>
</thead>
<tbody>
<tr><td>Back to home</td></tr>
<tr><td>Dynamically search in Well Surfer Database</td></tr>
</tbody>
</table>
{% endblock %}
my goal is to have a link that takes me back to the homepage.
Is there a more elegant way of creating the link other than hardcoding the "http://" in front of the "{{ request.get_host }}" in the template?
Thanks

Related

Write reusable template part django

In my templates, in many places, I've got repeating parts, like:
<th class="column-title">
<a href="?{% query_transform order_by='x' %}">
{{ objForm.x.label_tag }}</a>
</th>
<th class="column-title">
<a href="?{% query_transform order_by='y' %}">
{{ objForm.y.label_tag }}</a>
</th>
<th class="column-title">
<a href="?{% query_transform order_by='z' %}">
{{ objForm.z.label_tag }}</a>
</th>
Is there a way, to write some "function" to render such html part like: (pseudocode)
in html:
render("x")
render("y")
render("z")
in python:
def render(param):
return " <th class="column-title">
<a href="?{% query_transform order_by='+param+' %}">
{{ objForm'+param+'.label_tag }}</a>
</th>"
PS. I know, that theoreticaly I can prepare ordered list in view, and then iterate over it in a template, but I think it is not a good solution ,as I prefer to build my view, fields order etc on the template side.
You can use the include template tag to insert your common template code where you need it.
<table>
<thead>
<tr>
{% include "table_header.html" with field=objForm.x %}
{% include "table_header.html" with field=objForm.y %}
{% include "table_header.html" with field=objForm.z %}
<tr>
</thead>
</table>
and
<a href="?{% query_transform order_by=field.name %}">
{{ field.label_tag }}
</a>

How to read list in django template

Could someone shed a light to help me to solve this nested list problem in Django template.
I come across a list as such
[['Summary 1 CODE018'], [['Directory/File Name', 'Result']], [[['dir/var1/file1.txt', 'pass'], ['dir/var2/file2.txt', 'pass'], ['dir/var1/file3', 'pass']]], [[['null']]]]
How do I loop the above list so that I can get a table such as the first index is the table title, index1 is the table title and the rest (except when is it null) is the table body? Please take note that the last item could be [[['null']]] or [[['null', 'Info on directory CODEA18']]] which should be skip when null is detected.
Expected table:
Directory/File Name Result
dir/var1/file1.txt pass
dir/var2/file2.txt pass
dir/var3/file3.txt pass
My faulty code as below
<table id="myTable">
{% for list in summary %}
<tr>
<td> {{ list.0 }} </td>
<tbody>
<td> {{ list.0.0 }} </td>
<td> {{ list.0.0.0 }} </td>
</tbody>
</tr>
</table>
You list have 4 items. In that 2nd is Table heading and 3rd is table row.
We need to run 2 loops, one for thead and one for tbody
<table border>
<thead>
<tr>
{% for i in a[1][0] %}
<th>{{i}}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for i in a[2][0] %}
<tr>
{% for j in i%}
<td>{{j}}</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>

Html table rendering

My Django view is returning a list of dictionaries. This goes to html table through template rendering. Below is my template code,
My list of dictionary looks like below,
Results :
[{ 'name':'x','age': 20}, {'name': 'y','age': 25 }]
<table class="table table-striped" border="1" class="dataframe">
<thead>
<tr style="text-align: center;">
{% for k, v in results.0.items %}
<th>{{ k }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for x in results %}
<tr style="text-align: center;">
{% for y in x %}
<td> {{ x.y }} </td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
Expected output:
name age
x 20
y 25
But the output is blank.
Please let me know if there is anything wrong with my HTML table template.
You can use items properly in your inner loop just like the outer loop
<div>
<table class="table table-striped" border="1" class="dataframe">
<thead>
<tr style="text-align: center;">
{% for k, v in results.0.items %}
<th>{{ k }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for x in results %}
<tr style="text-align: center;">
{% for i,j in x.items %}
<td> {{ j }} </td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
</div>

Adding rows breaks datatable in Django

I have a datatable in Django that functions fine when hardcoded, but when I add my Django template tags it breaks. The inspect on the page says: Uncaught TypeError: Cannot set property '_DT_CellIndex' of undefined in jquery.datatables.min.js
This only happens when I have more than one user in the table, or try to add a column in the table with django. Since I will be using multiple datatables in my project, I need to figure out what I'm doing wrong. The datatable JS code I'm using comes from a template, and I'm not sure if the error is in the template code or in my Django template code. So please excuse the long code blocks.
employees.html (django template):
<div class="card-body collapse in">
<div class="card-block card-dashboard">
<button id="addRow" class="btn btn-primary mb-2 js-create-employee"><i class="ft-plus"></i> Add New Employee</button>
<table class="table table-striped table-bordered zero-configuration">
<thead>
<tr>
<th>Name</th>
<th>Username</th>
<th>Roles</th>
<th>Email</th>
<th>Mobile Contact</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{% for profile in user_profile_list %}
<tr>
{% if not profile.user.is_superuser %}
<td>{{ profile.user.get_full_name }}</td>
<td>{{ profile.user.username }}</td>
<td>
{% for g in profile.user.groups.all %}
<div class="tag tag-default">{{ g.name|split:'_'|title }}</div>
{% endfor %}
</td>
<td>{{ profile.user.email }}</td>
<td>{{ profile.mobile_phone }}</td>
<td><i class="fa fa-pencil"></i> <a href="#" alt="Assign"><i class="fa fa-link"></i><a/> <a href="#" alt="delete"><i class="fa fa-times"></i><a/></td>
{% endif %}
</tr>
{% endfor %}
</tbody>
<tfoot>
<tr>
<th>Name</th>
<th>Username</th>
<th>Roles</th>
<th>Email</th>
<th>Mobile Contact</th>
<th>Actions</th>
</tr>
</tfoot>
</table>
Datatable instantiation:
$(document).ready(function() {
/****************************************
* js of zero configuration *
****************************************/
$('.zero-configuration').DataTable();
});
Jquery.datatables.min.js and dataTables.bootstrap4.min.js are also used, but those come stock from bootstrap 4. I'm not going to add them here unless needed, and they are minified anyway.
This issues occurs only when data is not available for the table or tags.
You have conditions in template using django templatestag So the number of every <td> element in your table that is a child of a <tr> element doesn't match the number of <th> elements that are a child of the element.
Please make sure you have same number of <td> tag in <tbody> tag.
EDIT:
Maybe {% if not profile.user.is_superuser %} this condition creating this issue. If user is superuser then no <td> tag will create that will not match same number of <th> in <thead>

Django Datatables view

Im trying to convert my tables in my django app to datatables using django-tables2.
Im my campaigns.py view I have:
class CampaignListView(FacebookAdInit):
""" CampaignListView for viewing all the campaigns"""
def get(self, request, *args, **kwargs):
ad_account = self.get_ad_account(kwargs.get('ad_account_id'))
campaigns = self.get_campaigns(ad_account.get('id')) \
if ad_account else None
context = {'campaigns': campaigns, 'ad_account': ad_account}
return render(request, 'app/campaigns/index.html', context)
Im my campaigns/index.html I have:
{% extends "app/empty_page.html" %}
{% load render_table from django_tables2 %}
{% block content %}
{% if ad_account %}
{% render_table context %}
{% endif %}
{% endblock %}
However this gives me the error: Expected table or queryset, not 'str'.
ANy help will be greately appreciated.
Right now I generate the table using this piece of code:
<table class="table table-bordered table-striped" id="campaigns">
<thead>
<tr>
<th> #</th>
<th> Campaign Name</th>
<th> Campaign Objective</th>
<th> Campaign Effective Status</th>
</tr>
</thead>
<tbody>
{% for campaign in campaigns %}
<tr>
<td> {{ forloop.counter }} </td>
<td>
<a href="/ad_accounts/{{ ad_account.id }}/campaigns/{{ campaign.id }}/ad_sets">
{{ campaign.name }} </a>
</td>
<td> {{ campaign.objective }}</td>
<td> {{ campaign.effective_status }} </td>
</tr>
{% endfor %}
</tbody>
</table>
You should pass a Table instance or queryset to the render_table tag.
{% render_table campaigns %}