Robin
Robin

Reputation: 5486

how to query for the full name in django

I have a template which will display all the likes and the person liked for a particular forum. In the template it can display numbers of likes and all the person's username that liked that forum. But I want the full name and not the username (here it is the email). How do I get the full name in the template or if possible from the view itself. Thank you.

forums.html:

{% extends "base.html" %}
{% load forum_tags %}

{% block content %}
<h2>Logged in as -- {{request.user}}</h2>
<h1>Forums:</h1>
    {% if forums.count > 0 %}
        {% for forum in forums %}
            <h2><a href="/forum/get/{{forum.id}}/">{{forum.question}}</a></h2>
            <p>{{forum.body | truncatewords:"30"}}</p>
            {% if user in forum.likes.all and forum.likes.count > 1 %}
                <p><a href="/forum/delete_likes/{{forum.id}}/">Unlike</a> You and {{forum.likes.count | substract:1}} others liked</p>
            {% elif user in forum.likes.all %}
                <p>You liked it</p>
            {% else %}
                <p><a href="/forum/update_likes/{{forum.id}}/">Like</a></p>
            {% endif %}
            {% for likes in forum.likes.all %}
                <li><a href="/get/{{likes.user}}">{{likes}}</a></li>
            {% endfor %}
        {% endfor %}
    {% else %}
        <p>Sorry! No forum to display.</p>
    {% endif %}

{% endblock %}

snippet of views.py:

def forums(request):
    forums = Forum.objects.all()
    c = {'forums': forums}
    return render(request, 'forums.html', c)

Upvotes: 1

Views: 273

Answers (1)

Thomas Orozco
Thomas Orozco

Reputation: 55303

If you're using the default User model from django.contrib.auth.models, it has a get_full_name method that you can use in your template:

{{ user.get_full_name }}

Otherwise, you can implement that method in your own User model too. Any method that accepts no arguments can be called from templates (unless they have a alters_data attribute set to True).

Upvotes: 2

Related Questions