用户 his/herself 和其他用户的两个单独的模板配置文件页面?
Two separate template profile pages for user his/herself and other users?
由于用户的个人资料页面应该是可编辑的 himself/herself,
我是否应该确定视图中的个人资料所有者并为查看同一页面的其他用户使用不同的模板
或
我应该使用模板标签来确定当前用户是否是个人资料所有者吗?
我是网络应用程序开发和 Django 的新手。抱歉,如果问题太宽泛。谢谢
您根据用户 ID 显示 himself/herself 用户个人资料数据,因此单个个人资料模板就足够了。
示例:
def profile(request,userid)
.......
return render_to_response('profile.html',{..context..})
您可以使用单个模板并检查用户是否在您的模板中进行了身份验证,并为登录用户显示必要的代码。
要检查用户是否在模板中进行了身份验证,请使用 user.is_authenticated
。但是,请记住,必须在设置中启用 auth
context processor 才能使当前 user
出现在模板上下文中。
您可以在 url 中传递 user_id
kwarg 以访问该用户的个人资料页面。您可以像这样定义 urls:
url(r'^user/profile/(?P<user_id>\w+)/$', ProfilePage.as_view(), name='profile_page'),
然后在你的views
中,你可以在上下文中传递requested_profile_id
。
Class ProfilePage(..):
def get_context_data(self):
context = super(ProfilePage, self).get_context_data()
# pass user id for which profile page was requested
context['requested_profile_id'] = self.kwargs.get('user_id')
return context
然后在您的模板中,检查当前用户的 id
是否与 requested_profile_id
相同。如果相同,则可以显示要编辑配置文件的部分。你可以这样做:
my_template.html
...
<!-- your normal code here -->
..
{% if user.is_authenticated and user.id==requested_profile_id %}
...
<!-- your code for current user profile edit page here -->
..
{% endif %}
由于用户的个人资料页面应该是可编辑的 himself/herself,
我是否应该确定视图中的个人资料所有者并为查看同一页面的其他用户使用不同的模板
或
我应该使用模板标签来确定当前用户是否是个人资料所有者吗?
我是网络应用程序开发和 Django 的新手。抱歉,如果问题太宽泛。谢谢
您根据用户 ID 显示 himself/herself 用户个人资料数据,因此单个个人资料模板就足够了。 示例:
def profile(request,userid)
.......
return render_to_response('profile.html',{..context..})
您可以使用单个模板并检查用户是否在您的模板中进行了身份验证,并为登录用户显示必要的代码。
要检查用户是否在模板中进行了身份验证,请使用 user.is_authenticated
。但是,请记住,必须在设置中启用 auth
context processor 才能使当前 user
出现在模板上下文中。
您可以在 url 中传递 user_id
kwarg 以访问该用户的个人资料页面。您可以像这样定义 urls:
url(r'^user/profile/(?P<user_id>\w+)/$', ProfilePage.as_view(), name='profile_page'),
然后在你的views
中,你可以在上下文中传递requested_profile_id
。
Class ProfilePage(..):
def get_context_data(self):
context = super(ProfilePage, self).get_context_data()
# pass user id for which profile page was requested
context['requested_profile_id'] = self.kwargs.get('user_id')
return context
然后在您的模板中,检查当前用户的 id
是否与 requested_profile_id
相同。如果相同,则可以显示要编辑配置文件的部分。你可以这样做:
my_template.html
...
<!-- your normal code here -->
..
{% if user.is_authenticated and user.id==requested_profile_id %}
...
<!-- your code for current user profile edit page here -->
..
{% endif %}