我有一个问题。我有一个博客,用户可以登录并发表评论。当他们发表或发表评论时,文字会在右侧以链接的形式显示其名称(参见图片)。现在,我想要一个显示电子邮件名称等的用户配置文件页面。因此,我必须从第一个模板中获取名称并使用它。现在,我可以获取它们的名称了:),但我不知道如何使用它们。例如,用户名是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%}
在这里,我想获得他的信息。
user_profile.html
{% extends "base.html" %}
{%block content%}
{%load static%}
<div class="card float-left">
<img src="{% static 'images/img.jpg' %}" alt="John" style="width:100%">
{%if user.is_authenticated%}
<h3>{{user.first_name}} {{user.last_name}}</h3>
<p>{{user.email}}</p>
{%endif%}
<p class="title">CEO & Founder, Beehive</p>
<p>Istanbul Technical University</p
<p><button>Contact</button></p>
</div>
当然现在它会显示已登录用户的信息,但是我想根据他单击的用户来对其进行更改
编辑
现在我正在尝试从第一个模板中获取两个模型和数据,以使用cbv显示在第二个模板上。这是我的新观点。
class UserProfile(ListView):
template_name = 'user_profile.html'
model=PostModel
user=User.objects.get(username=username)
def get_context_data(self, **kwargs):
context = super(UserProfile, self).get_context_data(**kwargs)
context['posts'] = PostModel.objects.filter(author=user).order_by('created_on')
context['comments'] = CommentModel.objects.filter(author=user).order_by('created_on')
context['profile']=user
return context
但是在这里我得到了错误: 名称“用户名”未定义
答案 0 :(得分:1)
您必须查找用户,例如:
@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_view(userobject, username)
函数)。
答案 1 :(得分:0)
@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})
我解决了问题,所以我很高兴:)