[英]How to show specific field only to the user profile owner in graphene-django?
[英]How to show profile editing features only to account's owner in Django?
我正在使用Django 1.8.4构建网站,并使用django-registration-redux处理用户的登录和注销。
当所有用户都注销后,我手动进入用户的个人资料页面,其中包含用户信息和编辑个人资料的链接:
url(r'^users/(?P<slug>\w+)/$', UserProfileDetailView.as_view(), name="profile")
问题是,当我访问用户页面(即/ users / james /)时,它会将我识别为已登录( is_authenticated
)的用户“ james”,并显示注销和编辑配置文件按钮,但事实并非如此! 它应该只显示公共信息。 (当我单击“编辑”按钮时,它会要求我提供登录信用,以便该部分正常工作)
如何避免这种情况(显示来自随机登录或匿名用户的编辑配置文件,注销等),而仅将其显示给已登录的帐户所有者?
viws.py
class UserProfileDetailView(DetailView):
model = get_user_model()
slug_field = "username"
template_name = "user_detail.html"
def get_object(self, queryset=None):
user = super(UserProfileDetailView, self).get_object(queryset)
UserProfile.objects.get_or_create(user=user)
return user
user_detail.html
<h2>{{ object.username }}'s Profile</h2>
{% if object.userprofile.bio %}
<div>
<b>Bio:</b>
{{ object.userprofile.bio }}
</div>
{% endif %}
{% if object.username == user.username and user.is_authenticated %}
<p><a href='{% url "edit_profile" %}'>Edit My Profile</a></p>
{% endif %}
#footer
{% if user.is_authenticated %}
<a href="{% url 'logout' %}">Logout</a> |
<a href="{% url 'profile' slug=user.username %}"><b>{{ user.username }}</b></a>
{% else %}
<a href="{% url 'registration_register' %}">Register</a> |
<a href="{% url 'login' %}">Login</a>
{% endif %}
模板上下文中的“用户”变量被正在查看的当前记录覆盖。
尝试改变
{% if object.username == user.username and user.is_authenticated %}
至
{% if object == request.user and request.user.is_authenticated %}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.