[英]How can I see user information other than the logged-in user in Django?
I'm using Django 2.2 and PostgreSQL. 我正在使用Django 2.2和PostgreSQL。 After logging in, the user has a page where they can see other users' information.
登录后,用户将看到一个页面,他们可以在其中查看其他用户的信息。 However, all users' login information appears on the page.
但是,所有用户的登录信息都会显示在页面上。 I want to see information from other users except him.
我想查看除他以外的其他用户的信息。 How can I do that?
我怎样才能做到这一点?
templates/neighbor.html templates / neighbor.html
{% csrf_token %}
{% if neighbor_list %}
{% for neighbor in neighbor_list %}
<div class="card border-left-success py-2" style="background-color: rgb(240,240,240);">
<div class="card-body">
<div class="row no-gutters align-items-center">
<div class="col mr-2">
<div class="text-left">
<strong><p><a href="{% url 'store:neighbor_detail' neighbor.user.username %}">{{neighbor.user.first_name}} {{neighbor.user.last_name}}</a></p></strong>
<p><strong><i class="fa fa-user"> : </i></strong>{{neighbor.user.username}}</p>
<p><strong><i class="fa fa-envelope"> : </i></strong>{{neighbor.user.email}}</p>
<p><strong><i class="fa fa-phone"> : </i></strong>{{neighbor.phone}}</p>
<p><strong><i class="fa fa-fax"> : </i></strong>{{neighbor.fax}}</p>
{% if neighbor %}
<p><strong><i class="fa fa-map"> : </i></strong>{{neighbor.neighborhood}}, {{neighbor.avenue}}, {{neighbor.street}}, {{neighbor.block}}, No.{{neighbor.number}}, Kat.{{neighbor.storey}}, {{neighbor.district}}/{{neighbor.province}}</p>
<p>{{neighbor.profile_image}}</p>
{% endif %}
</div>
</div>
</div>
</div>
</div>
{% endfor %}
{% endif %}
store/views.py store / views.py
from store.models import StoreOtherInfo
def neighbor(request):
neighbor_list = StoreOtherInfo.objects.all()
return render(request,'store/neighbor.html',{"neighbor_list":neighbor_list"}
store/models.py store / models.py
class StoreOtherInfo(models.Model):
user = models.OneToOneField(User,on_delete=models.CASCADE,blank=True, null=True)
phone = models.CharField(max_length=11)
fax = models.CharField(max_length=11)
province = models.CharField(max_length=11)
district = models.CharField(max_length=11)
neighborhood = models.CharField(max_length=11)
...
def __str__(self):
return self.user.username
You can use exclude(user=request.user)
. 您可以使用
exclude(user=request.user)
。
from django.contrib.auth.decorators import login_required
@login_required
def neighbor(request):
neighbor_list = StoreOtherInfo.objects.all().exclude(user=request.user)
return render(request,'store/neighbor.html',{"neighbor_list":neighbor_list})
In the code above I've used the login_required
decorator to make sure the user is logged in. An alternative is to check user.is_authenticated
and only do exclude()
when the user is logged in. 在上面的代码中,我使用了
login_required
装饰器来确保用户已登录。另一种方法是检查user.is_authenticated
并且仅在用户登录时才进行exclude()
。
@login_required
def neighbor(request):
neighbor_list = StoreOtherInfo.objects.all()
if request.user.is_authenticated:
neighbor_list = neighbor_list.exclude(user=request.user)
return render(request,'store/neighbor.html',{"neighbor_list":neighbor_list})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.