Python 在django中访问用户详细信息

Python 在django中访问用户详细信息,python,html,django,django-views,Python,Html,Django,Django Views,我有个问题。我有一个博客,用户可以登录并发表评论。当他们发布或评论时,文本会以链接的形式出现在右侧(见图)。现在我想有一个userprofile页面,其中显示电子邮件名称等。因此,我必须从第一个模板中获取名称并使用它。现在我可以抓住它们的名字:)但我不知道如何使用它们。例如,用户名是alex。我可以在新模板上显示alex,但我需要的是类似的东西。alex.email或alex.name。多谢各位 view.py @login_required def user_profile(request,u

我有个问题。我有一个博客,用户可以登录并发表评论。当他们发布或评论时,文本会以链接的形式出现在右侧(见图)。现在我想有一个userprofile页面,其中显示电子邮件名称等。因此,我必须从第一个模板中获取名称并使用它。现在我可以抓住它们的名字:)但我不知道如何使用它们。例如,用户名是alex。我可以在新模板上显示alex,但我需要的是类似的东西。alex.email或alex.name。多谢各位

view.py

@login_required
def user_profile(request,username):
    return render(request, "user_profile.html",{'username':username})
home.html这是我想要获取他的名字的模板

{% extends "base.html" %}
{%block content%}
{% load crispy_forms_tags %}
<div class="container">
    <div class="row">
        <!-- Blog Entries Column -->
        <div class="col-md-8 mt-3 left mx-auto">
            {% for posts in postmodel_list %}
            <div class="card mb-4 block">
                <a class="overlay" href="{% url 'post_detail' posts.slug  %}"style="text-decoration:none"> </a>
                <div class="card-body inner">
                  <h2 class="card-title">{{ posts.post }}</h2>
                  <p style="text-align:right;" class="card-text text-muted h6"><a style="text-decoration:none" href="{%url 'user_profile' posts.author %}">@{{ posts.author }}</a> </p>
                </div>
            </div>
            {% endfor %}
          </div>
        </div>

</div>


<div class="col-md-4 float-right ">
  <button style= "position: fixed; bottom:50px;" type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@mdo">Add New Post</button>
</div>

<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
 <div class="modal-dialog" role="document">
   <div class="modal-content">
     <div class="modal-header">
       <h5 class="modal-title" id="exampleModalLabel">New Post</h5>
     </div>
     <div class="modal-body">
       <form method="post" style="margin-top: 1.3em;">
         {% csrf_token %}
         {{ form|crispy }}
         <div class="modal-footer">
           <button type="submit" class="btn btn-primary">Submit</button>
           <button type="submit" class="btn btn-secondary" data-dismiss="modal">Close</button>
         </div>
       </form>
     </div>
   </div>
 </div>
</div>




<style>
.card{
  box-shadow: 0 16px 48px #E3E7EB;
}
</style>

{%endblock content%}
但这里我得到了一个错误:
未定义名称“username”

您必须查找用户,例如:

@login_required
def user_profile(request, username):
    if request.user.username != username:
        user = User.objects.get(username=username)
    else:
        user = request.user
    return render(request, "user_profile.html", {'profile': user})
您需要在模板中使用
{{profile.username}}
等(Django为自己保留了
{{user.xxx}}
,并将覆盖它

您可能应该向该视图添加更多权限。如上所述,任何登录用户都可以查看任何其他登录用户的配置文件,即将if更改为:

if request.user.username != username and can_view(request.user, username):
    ....
(然后实现
can\u视图(userobject,username)
功能)


我解决了我的问题,我很高兴:)

非常感谢,你知道我如何在CBV中提供它,我在问题中编辑了它,但我的主要问题是从模板中获取数据并在同一模板中显示其他两个模型这是一个完全不同的问题。你应该把它作为一个新的问题来问,而不是改变当前的问题(并且使任何答案对未来的访问者都不可用)。
@login_required
def user_profile(request, username):
    if request.user.username != username:
        user = User.objects.get(username=username)
    else:
        user = request.user
    return render(request, "user_profile.html", {'profile': user})
if request.user.username != username and can_view(request.user, username):
    ....
@login_required
def user_profile(request, username):
    if request.user.username != username:
        user = User.objects.get(username=username)
    else:
        user = request.user
    posts=PostModel.objects.filter(author=user)
    comments=CommentModel.objects.filter(author=user)
    return render(request, "user_profile.html", {'profile': user,'posts':posts,'comments':comments})