Lets say I have code in template like this:
<a href="#"> {% if request.user.first_name or request.user.last_name %} {{ request.user.first_name }} {{ request.user.last_name }} {% else %} {{ request.user }} {% endif %} </a>
Problem with this code is that it adds trailing space to link, so link looks like link_ with underline at the end.
How do I remove such trailing spaces?
{% spaceless %}
tag doesn't quite help here because it only removes spaces between tags.
score:9
I actually found simple solution for my problem.
<a href="#">{% spaceless %}
{% if request.user.first_name or request.user.last_name %}
{{ request.user.first_name }} {{ request.user.last_name }}
{% else %}
{{ request.user }}
{% endif %}
{% endspaceless %}</a>
By placing spaceless tag inside it strips the string it gets. Placing outside
Similar question
- Django template link with if else statement
- Django Template Language: Using a for loop with else
- Django template on Google App Engine with if statement
- Django template nested for loop with if statement not working
- How to add this if statement to a template with Django and improve the views.py
- Django 1.7 sending password reset link to user with a html template
- Django else statement in html template not working
- Django template language "If" statement with many "and"
- Django Template Nested for with If Statement
- how to operate with values from dictionary in a condtional statement in a django template
score:0
Quick workaround: Use html comments to "escape" the unnecessary whitespace. Probably better solution: Create a template tag that holds this conditional.
score:0
Seconding the usage of a single template tag - it'd be good (and pretty easy) to remove this logic from the template.
Although, doesn't just using {{ request.user }}
give exactly the same result as what you're doing here?
score:0
Instead of if-else block try to use shorter version:
{{ user.get_full_name|default:user.get_username }}
score:1
As a possible variant of decision: http://www.soyoucode.com/2011/minify-html-output-django
Or you could try to create your own tag if there are no such tags: https://docs.djangoproject.com/en/dev/howto/custom-template-tags/
Credit To: stackoverflow.com
Related Query
- How to check a check box is checked with if statement in django template
- AngularJS with Django - Conflicting template tags
- Django template how to look up a dictionary value with a variable
- Django {% with %} tags within {% if %} {% else %} tags?
- Alternate Row Coloring in Django Template with More Than One Set of Rows
- Limit number of characters with Django Template filter
- How to pass an array in Django to a template and use it with JavaScript
- django template if or statement
- Having trouble with user.is_authenticated in django template
- Python / Django - If statement in template around extends
- Django inclusion tag with configurable template
- jquery template tags conflict with Django template!
- Django - How to use custom template tag with 'if' and 'else' checks?
- Pulling data to the template from an external database with django
- creating a MIME email template with images to send with python / django
- Dynamic URL with variable django template
- Django 'ManagementForm data is missing or has been tampered with' when saving modelForms with foreign key link
- Django url template with query parameters
- Generating a url with the same GET parameters as the current page in a Django template
- Grouping Conditions in Django template if statement
More Query from same tag
- How to unittest Session timeout in Django
- Apache HTTP Basic Auth - 401 error after supplying username and password
- Django StreamingHttpResponse into a Template
- Adding comments in django
- TypeError when passing dictionary arguments to a view through urls.py
- TypeError at /login Strings must be encoded before hashing
- AssertionError - no exception supplied - django
- In Django, how to find out what URL pattern matched for a request?
- Django DRF best practices to return response headers on every request
- django makemigrations to rename field without user input
- Sending form data from Angular to Django
- Unable to upload image to Django Project, getting Form object has no attribute 'save'
- How to filter objects in Django by time since last 24 hours?
- [django]when debug=false,MEDIA_URL returns not found
- Django render a DynamoDB JSON into a HTML table