Show string if value is None in template - django

If a value is None I would like to show a - rather than rendering None in the table.
However I get the following error:
Could not parse the remainder: ' or '-'' from 'employee.full_name or '-''
I'd like to avoid doing a ton of if else statements if possible.
Here's my code:
{% for employee in employees %}
<tr>
<td>{{employee.full_name or '-'}}</td>
<td>{{employee.position or '-'}}}</td>
<td>{{employee.dob or '-'}}}</td>
<td>{{employee.phone or '-'}}}</td>
<td>{{employee.email or '-'}}}</td>
<td>{{employee.address or '-'}}}</td>
<td>{{employee.joined or '-'}}}</td>
</tr>
{% endfor %}
The following Python code works as expected:
dog = None
print(dog or '-')

You can work with the |default_if_none template filter [Django-doc]:
{% for employee in employees %}
<tr>
<td>{{ employee.full_name|default_if_none:'-' }}</td>
<td>{{ employee.position|default_if_none:'-' }}</td>
<td>{{ employee.dob|default_if_none:'-' }}</td>
<td>{{ employee.phone|default_if_none:'-' }}</td>
<td>{{ employee.email|default_if_none:'-' }}</td>
<td>{{ employee.address|default_if_none:'-' }}</td>
<td>{{ employee.joined|default_if_none:'-' }}</td>
</tr>
{% endfor %}

Related

double curly braces in quote mark - Django

Could some one tell me the full picture of the use of the quote mark and the double curly braces?
<a href="{{ url_for('delete_event', id=event.id) }}" class="text-danger">
The full code is as below. Is it Django synatx {{ ... }} embeded?
{% for event in events %}
<tr>
<td>{{ event.event }}</td>
<td>{{ event.start_time }}</td>
<td>{{ event.end_time }}</td>
<td>{{ event.position }}</td>
<td>Delete</td>
</tr>
{% endfor %}
You need to correct url in template like this django does not support jinja tag, but django have a built in template language is called DTL (Django Template Language) so you need to correct in your code url pattern like this
{% for event in events %}
<tr>
<td>{{ event.event }}</td>
<td>{{ event.start_time }}</td>
<td>{{ event.end_time }}</td>
<td>{{ event.position }}</td>
<td>Delete</td>
</tr>
{% endfor %}

How to ADD for loop result in templates

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'm trying to use data pulled from CoinMarketCap API, I can't access all the information from the dictionary

It looks like the information is a List with embedded dictionary. I'm a beginner and don't fully understand how to pull the information from lists/dictionaries.
EXP:
'data': [{
'id': 1,
'name': 'Bitcoin',
'symbol': 'BTC',
'slug': 'bitcoin',
'num_market_pairs': 7956,
'date_added': '2013-04-28T00:00:00.000Z',
'tags': ['mineable'],
'max_supply': 21000000,
'circulating_supply': 18344737,
'total_supply': 18344737,
'platform': None,
'cmc_rank': 1,
'last_updated': '2020-04-25T16:09:51.000Z',
'quote': {
'USD': {
'price': 7582.532132,
'volume_24h': 33998463530.3441,
'percent_change_1h': -0.102004,
'percent_change_24h': 0.497048,
'percent_change_7d': 5.20237,
'market_cap': 139099557755.5893,
'last_updated': '2020-04-25T16:09:51.000Z'
}
}
}
I can pull the information in 'data', but I can't pull the information in the 'quote':{'USD'} portion of the dictionary. My code in my template is:
{% for coin_Key in cmc_Data.data %}
{{ coin_Key }}
<tr>
<td>{{ coin_Key.cmc_rank }}</td>
<td>{{ coin_Key.name }}</td>
<td>{{ coin_Key.symbol }}</td>
<td>{{ coin_Key.quote.price }}</td>
<td>{{ coin_Key.quote.market_cap }}</td>
<td>{{ coin_Key.total_supply }}</td>
<td>{{ coin_Key.max_supply }}</td>
</tr>
{% endfor %}
the {{ coin_Key }} lists all the information, so I know it's pulling from the API properly.
I'm not sure I explained this properly, hit me up with any questions and I'll do my best to clarify.
You're almost there, it looks like you just missed the USD object after quote.
{% for coin_Key in cmc_Data.data %}
{{ coin_Key }}
<tr>
<td>{{ coin_Key.cmc_rank }}</td>
<td>{{ coin_Key.name }}</td>
<td>{{ coin_Key.symbol }}</td>
<td>{{ coin_Key.quote.USD.price }}</td>
<td>{{ coin_Key.quote.USD.market_cap }}</td>
<td>{{ coin_Key.total_supply }}</td>
<td>{{ coin_Key.max_supply }}</td>
</tr>
{% endfor %}

Accessing variable context name in template

I'm just discovering Django and i'm still a total noob.
I got a view that return multiple query set.
class RouteDateView(ListView):
model = Tblsummary
template_name = 'reviewrouteassess/selectdaterange.html'
def get_context_data(self, *args, **kwargs):
context = super(RouteDateView, self).get_context_data(*args, **kwargs)
context['allroute'] = Tblsummary.objects.filter(off_no=self.request.GET.get('depotcode')[:4])
for p in context['allroute']:
context[p.res_route] = Tblsummary.objects.filter(res_route=p.res_route)
return context
Then in my template I would like to access those dynamically name context. That's where i'm stuck.
<table>
{% for a in allroute %}
<tr>
<td>{{ a.res_route }}</td>
<td>{{ a.hcalls }}</td>
</tr>
{% for d in a.res_route %}
<tr>
<td>{{ d.res_route }}</td>
<td>{{ d.hcalls }}</td>
</tr>
{% endfor %}
{% endfor %}
How can I evaluate a.res_route so that a.res_route return the context passed by the view?? Thanks you so much!!
{% for d in a.res_route %}
I would suggest adding a method to your model to access the second query:
class Tblsummary(models.Model):
... # or whatever is there currently
def sub_routes(self): # name this how you'd like
return Tblsummary.objects.filter(res_route=self.res_route)
And then in your template:
<table>
{% for a in allroute %}
<tr>
<td>{{ a.res_route }}</td>
<td>{{ a.hcalls }}</td>
</tr>
{% for d in a.sub_routes %}
<tr>
<td>{{ d.res_route }}</td>
<td>{{ d.hcalls }}</td>
</tr>
{% endfor %}
{% endfor %}
This isn't going to be ideal from an efficiency standpoint since the sub_routes query is called once per entry in allroute but right now allroute is limited to 4 results so that shouldn't be an issue in practice.

How to add two for loops in a single table body in django template

<thead>
<th> username </th>
<th>place</th>
</thead>
{% for i, j in user_group1, user_group2 %}
<tbody>
{% if i %}
<td>{{ i.username }}</td>
<td>{{ i.place }}</td>
{% else %}
<td>{{ j.username }}</td>
<td>{{ j.place }}</td>
{% endif %}
</tbody>
{% endfor %}
I want to use two for loops in a sinle table body. First i need to start the first one and after that i need to start the next one. how can i do this
If you're using Jinja2, you can join the two lists into one with the + operator:
{% for i in user_group1|list + user_group2|list %}
<tbody>
<td>{{ i.username }}</td>
<td>{{ i.place }}</td>
</tbody>
{% endfor %}