Jason G
Jason G

Reputation: 200

How do I view the first name of a user in Django?

I have set up my blog so that when a user clicks on the displayed name, it will go to that user's blogs (filtered that so only that person's blogs will show up). I used {{ view.kwargs.username }} to show the person's username, but I'd rather show the first name. What can I change to accomplish this?

blog/templates/blog/user_post.html:

{% extends 'blog/base.html' %}
{% block content %}
<h1 class="mb-3">Posts by {{ view.kwargs.username }} ({{ page_obj.paginator.count }})</h1>
{% for post in posts %}
    <article class="media content-section">
        <img class="rounded article-img mt-2" src="{{ post.author.profile.image.url }}" alt="">
        <div class="media-body">
            <div class="article-metadata">
                <a class="mr-2" href="{% url 'user-posts' post.author.username %}">{{ post.author.profile.user.first_name }} {{ post.author.profile.user.last_name }}</a>
                <small class="text-muted">{{ post.date_posted|date:"F d, Y" }}</small>
            </div>
            <h2><a class="article-title" href="{% url 'post-detail' post.id %}">{{ post.title }}</a></h2>
            <p class="article-content">{{ post.content }}</p>
        </div>
    </article>
{% endfor %}
{% if is_paginated %}
    {% if page_obj.has_previous %}
        <a class="btn btn-outline-info mb-4" href="?page=1">First</a>
        <a class="btn btn-outline-info mb-4" href="?page={{ page_obj.previous_page_number }}">Previous</a>
    {% endif %}
        {% for num in page_obj.paginator.page_range %}
            {% if page_obj.number == num %}
                <a class="btn btn-info mb-4" href="?page={{ num }}">{{ num }}</a>
            {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
                <a class="btn btn-outline-info mb-4" href="?page={{ num }}">{{ num }}</a>
            {% endif %}
        {% endfor %}

    {% if page_obj.has_next %}
        <a class="btn btn-outline-info mb-4" href="?page={{ page_obj.next_page_number }}">Next</a>
        <a class="btn btn-outline-info mb-4" href="?page={{ page_obj.paginator.num_pages }}">Last</a>
    {% endif %}
{% endif %}

{% endblock content %}

blog/urls.py:

path('user/<str:username>/', UserPostListview.as_view(), name='user-posts'),

blog/views.py:

class UserPostListview(ListView):
    model = Post
    template_name = 'blog/user_posts.html'
    context_object_name = 'posts'
    # ordering = ['-date_posted']
    paginate_by = 10

    def get_queryset(self):
        user = get_object_or_404(User, username=self.kwargs.get('username'))
        return Post.objects.filter(author=user).order_by('-date_posted')

Upvotes: 0

Views: 178

Answers (1)

sin tribu
sin tribu

Reputation: 1180

You can use a django templatetags:

In your app, create a directory called templatetags and create a file

myapp/templatetags/mytemplates.py

from django import template 

def extractFirstName( username ):
    return username.split()[0] # or whatever to return the first name

register = template.Library()
register.filter( 'extractFirstName', extractFirstName )

Then in your html

{% extends 'blog/base.html' %}
{% load mytemplates %}
{% block content %}
<h1 class="mb-3">Posts by {{ view.kwargs.username | extractFirstName }} ...>
<!-- all your other stuff -->
{% endblock %}

Upvotes: 1

Related Questions