python - 如何在 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')
解决方案
您可以使用 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 %}
推荐阅读
- javascript - 练习 JavaScript 第二练习:TypeError: (0 , _leap.isLeap) 不是来自的函数
- android - Kotlin 中的对象失去它的 var 值?
- reactjs - 在所有 axios 实例中使用的 axios 中间件
- region - 谷歌数据洞察地区城市
- javascript - 为什么 HTML 布局在 769 像素处中断?
- ionic-framework - 离子选择弹出界面不会在硬件后退按钮操作中隐藏
- jquery - 如何在浏览器窗口上按 F5(Refressing)时停止页面重新提交
- elasticsearch - 在elasticsearch中按日期之间的时差查询
- ionic-framework - 使用 Crosswalk Webview 打开外部链接时出现应用程序错误
- excel - 比较不同工作表中的多个列并删除