![](/img/trans.png)
[英]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.