185👍
I know it’s a bit late, but you can use capfirst:
{{ "waiting for action"|capfirst }}
This will result into "Waiting for action"
- [Django]-How to override and extend basic Django admin templates?
- [Django]-Import data from excel spreadsheet to django model
- [Django]-Django MultiValueDictKeyError error, how do I deal with it
33👍
This solution also works if you have multiple words (for example all caps):
{{ "ALL CAPS SENTENCE"|lower|capfirst }}
This will output “All caps sentence”.
- [Django]-How do I do a not equal in Django queryset filtering?
- [Django]-Django models: default value for column
- [Django]-Django Cannot set values on a ManyToManyField which specifies an intermediary model. Use Manager instead
8👍
The title
filter works fine, but if you have a many-words string like: "some random text"
, the result is going to be "Some Random Text"
. If what you really want is to uppercase only the first letter of the whole string, you should create your own custom filter.
You could create a filter like this (follow the instructions on how to create a custom template filter from this doc – it’s quite simple):
# yourapp/templatetags/my_filters.py
from django import template
register = template.Library()
@register.filter()
def upfirstletter(value):
first = value[0] if len(value) > 0 else ''
remaining = value[1:] if len(value) > 1 else ''
return first.upper() + remaining
Then, you should load the my_filters file at your template, and use the filter defined there:
{% load my_filters %}
...
{{ myname|upfirstletter }}
- [Django]-Separating form input and model validation in Django?
- [Django]-Missing Table When Running Django Unittest with Sqlite3
- [Django]-Django project models.py versus app models.py
6👍
It worked for me in template variable.
{{ user.username|title }}
If the user is “al hasib” then the it will return “Al Hasib”
or
{{ user.username|capfirst }}
If user is ‘hasib’ then the last one will return “Hasib”
Both look something like same but there’s some differences.
- [Django]-Django rest framework change primary key to use a unqiue field
- [Django]-Is this the right way to do dependency injection in Django?
- [Django]-Add additional options to Django form select widget
- [Django]-How to set a value of a variable inside a template code?
- [Django]-The QuerySet value for an exact lookup must be limited to one result using slicing. Filter error
- [Django]-How to specify an IP address with Django test client?
2👍
Just use {{myname | capfirst}}
In Django the template filter capfirst capatialize the first letter of a given string.
- [Django]-Django dump data for a single model?
- [Django]-How to filter objects for count annotation in Django?
- [Django]-Django urlsafe base64 decoding with decryption