首页 > 解决方案 > 如何在 Django 中查看用户的名字?

问题描述

我已经设置了我的博客,以便当用户单击显示的名称时,它将转到该用户的博客(过滤后,只有该人的博客才会显示)。我曾经{{ view.kwargs.username }}显示此人的用户名,但我更愿意显示名字。我可以改变什么来实现这一点?

博客/模板/博客/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 %}

博客/urls.py:

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

博客/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')

标签: pythondjango

解决方案


您可以使用 django 模板标签:

在您的应用程序中,创建一个名为 templatetags 的目录并创建一个文件

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 )

然后在你的html中

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


推荐阅读