I have a model called Profile. I want to create a custom filter tag based on this idea: Profile.objects.filter(user=request.user)
Then I can use this filter within the template. Just complete this function: `
from django import template
register = template.Library()
from ..models import Profile
@register......
def filter_profile():
....
Then how to use it from the template.
And thanks.
If you want to show items that belong to the current user, you can do:
{% if user == request.user%}
<!-- Do something -->
{% endif %}
This will show only items that are related to the current user.
Assuming that you have the Profile model with a user one-to-one field (which is the most likely scenario) you would not need to create such a filter at all. You can access related models in the template directly. Here's an example:
model:
class Profile(models.Model):
...
user = models.OneToOneField(User, ...)
...
template:
{{ request.user.profile }}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.