Django templates - passing variable value in template to templatetags filter
class name id, first and last attributes
In my view.py, I retrieve the name object from the database and pass it to the index.html template.
In mine templagetags/my_tags.py
, I have a filter my_private_tag(value, arg)
that takes a value and an arg. It adds arg to the value and returns the result.
def my_private_tag(value, arg):
return value + ' ' + arg
In my index.html tag, I need to do the following.
{% if name %}
# to display
# John says hello
{{name.first | my_private_tag:"says hello"}
# Johns student id = id_value
{{name.first | my_private_tag:"????????"}
# Johns student id = id_value, lastname = lastname_value
{{name.first | my_private_tag:"????????"}
{% endif %}
Where:
id_value = name.id & lastname_value = name.last
Please fill out the form ???????? if you can.
a source to share
Instead of using your own filter for that. Why not do it like this:
{{name.first }} says hello
{{name.first }} student id = {{ name.id }}
This is more readable. And this is how you should use templates anyway.
If you still want to use a custom filter for this, you can pass this variable like this:
{{name.first|my_private_tag:name.id}}
a source to share
If the filter only receives two rows, it will not have access to the original name object, so there is no way to get the ID.
I think you are headed in the wrong direction, wanting to do this with a filter, where you can use existing tags to get the desired result. But, for the sake of argument, let's say that you could get
Johns student id = 47, lastname = Doe
from the filter. How would you do it? First, you need to pass the name object to the filter
{{name|my_private_tag:"??????"}}
Then the filter code will be
return name.first_name + "s student id = " + name.id + ", lastname = " + name.last_name
Note that you are not using an argument. If you want your filter to return different things depending on the argument, add conditional logic and let go.
a source to share
If you need a value that is solely a combination of model field values dictated by business logic, templatetag is not the right way to go.
Instead, it should be a property of the model:
class Student(models.Model):
firstname = models.CharField(...)
lastname = models.CharField(...)
@property
def student_id(self):
return '{}{}'.format(self.id, self.lastname)
You can use this property all over the world, not only in your templates, but also in views, exports, admin UI, etc. Which you probably need to see, which student_id
sounds like a pretty important attribute.
In the template:
{% if student %}
# to display
# John says hello
{{ student.firstname }} says hello
# John student id
Student ID: {{ student.student_id }}
{% endif %}
Templatetags should be created for more general functionality and functionality that depends on the current request, not something you've ever applied to just one particular model in a particular context. And more importantly, they are not intended for functionality that is not limited to templates.
a source to share