Remove minus sign from number in django tmplate - django

In my template I have this:
{{ gainer.total_progression }}
Which produces -5664
I need 5664
I tried
{{ gainer.total_progression }}|slice:"1:"
but it won't remove the -.
What is the proper way to handle this?

First, your code is wrong, it should be:
{{ gainer.total_progression|slice:"1:" }}
Also, you could use cut instead of slice:
{{ gainer.total_progression|cut:"-" }}

If you're not against installing separate library for this, you can use django-mathfilters.
{{ gainer.total_progression|abs }}

Related

Flask url_for in form action

I have a wtform in my html file, something like this:
{{ wtf.quick_form(form, action="/add/", method="post", extra_classes="form-horizontal",
role="form", form_type="basic") }}
(I actually notice I can omit action). So this works.
Now - if I change it to
action="{{ url_for('add') }}"
, I'm ending up in this url:
http://127.0.0.1:5000/add/%7B%7B%20url_for('add')%20%7D%7D
If I just create a link in html like so:
link
It does work and it gets me to /add/. I was wondering what the difference is.
Thanks!
instead of
action="{{ url_for('add') }}"
try to assign the output of url_for('add') function/helper to action
action=url_for('add')
without {{ and }}
so
{{ wtf.quick_form(form, action=url_for('add'), method="post", extra_classes="form-horizontal", role="form", form_type="basic") }}
Remember not to put commas in the tags ;)

Django custom template filter feeding cookie or default value

I'm designing a custom django filter, just to make sure it works I have something like this
{{ "Sleeps:"|translate:"fr" }}
and it works.
Now in the final implementation, I want it to get a cookie or else use a default value
{{ pg.title|translate:request.COOKIES.lang|default:"en" }}
I'm getting this error
VariableDoesNotExist at /chalet/belle-chery
Failed lookup for key [lang] in {'_ga': 'GA1.1.1026479868.1547798010', 'cookie-policy': 'true', 'csrftoken': 'VrVrvgZUfFrWhFDFjLIvZgOus9NrmjDx1JwNP2lzvz2FRAGmC1lLrKwiH4g31X5F', 'sessionid': 'ptp6smvt9w95qtqlkc7klx736u5k7uu5'}
so it doesn't implement the default part.
So I figure there's either a way to fix this or use middleware to set the cookie if it's not set.
Would be nice if it doesn't need middleware.
so it doesn't implement the default part.
It does, it applies the |default:"en" filter to the result of {{ pg.title|translate:request.COOKIES.lang }}, not to the request.COOKIES.lang expression.
The easiest way to solve this is probably defining a local variable, for example with the {% with ... %} template tag:
{% with lang=request.COOKIES.lang|default:"en" %}
{{ pg.title|translate:lang }}
{% endwith %}

Is there a quick way to grab a the ending of current page url? Django

I have a page. www.example.com/chat/testuser
I want to grab the username (testuser). I'm doing this within the template in order to pass along in an ajax call.
Is there a simple way? Something like {{ request.path }} (this returns full path).
{{ request.user }} might do the trick.
Edit:
Here's are some solutions using request.path. Assuming it returns the string "/chat/testuser", you can use one of these filters:
{{ request.path|cut:"/chat/" }}
{{ request.path|slice:"6:" }}
to remove the unwanted portion.

Error return truncatechars & safe by using Built-in template tags and filters

I try to Truncates the string and remove the html tags,
First, when I write it this way.
{{ post.context|safe }}
or
{{ post.context |truncatechars:100 }}
The left navigation bar shows normal.
But when I write this, this part of the HTML is gone.
{{ post.context |truncatechars:100|safe }}
But I can still find this Html in the source code.
So what can I do to get the correct results?thank you
If you just want to safely show content with HTML formatting.
{{ post.context|safe }}
If you truncate then some HTML tags may not get closed tag and you will get an irregular view.
If you want to strip HTML tags, you can strip by striptags and truncate characters using slice filters.
{{post.context|striptags|slice:':300'}}
Although it's kinda late to answer if you want to show the safe code with tags then use {{ post.context|truncatewords_html:30|safe }} or {{ post.content|truncatechars_html:100|safe }}. This won't break your code and will display your desired content.
No need to struggle around. You need to combine truncate, safe, and striptags
Respect the order below:
{{ string_variable|striptags|safe|truncate(100) }}
Does that help you?

How to get form fields' id in Django

Is there any way to get the id of a field in a template?
In the HTML I get: <input name="field_name" id="id_field_name"...
I know I can get the name with {{ field.html_name }}, but is there anything similar for getting the id?
Or can I only get it like this: id_{{ field.html_name }}?
You can get the ID like this:
{{ field.auto_id }}
You can also use id_for_label:
{{ field.id_for_label }}
This doesn't work for every form field.
For instance {{ form.address.auto_id }} works while {{ form.address.auto_name }} will not.
However you can use {{ form.address.html_name }} to get the equivalent answer.
Here are the docs
From the documentation-
each form field has an ID attribute set to id_<field-name>
, which is referenced by the accompanying label tag. This is important in ensuring that forms are accessible to assistive technology such as screen reader software. You can also customize the way in which labels and ids are generated.
So I would like to say id_field-name , you collect the field name from the model.
Here is the link to the documentation
In Django 2 you can retrieve the ID for a specific field using {{ field.id_for_label }}
This is documented here.