我正在尝试检查当前登录的用户与创建他们正在查看的页面的用户。因此,基本上,如果我登录了,我就不能转到另一个用户的帖子/个人资料,只需输入editurl模式就可以对其进行编辑。你知道吗
以下是我的观点:
class UserEditProfileView(LoginRequiredMixin,UpdateView):
login_url = '/login/'
model = UserProfile
fields = [
'first_name',
'profile_pic',
'location',
'title',
'user_type',
'about',
'website',
'twitter',
'dribbble',
'github'
]
template_name_suffix = '_edit_form'
def qet_queryset(self,request):
current_user = request.user.id
url_args = request.resolver_match.kwargs.pk
if current_user != url_args:
reverse('index')
我有get_queryset
函数和if语句在里面,检查当前登录的用户是否是他们试图编辑的配置文件的所有者,如果不是,则重定向他们。但是它没有做任何事情。。。我该如何实施这一点?你知道吗
更新
查看:
class UserEditProfileView(LoginRequiredMixin,UpdateView):
login_url = '/login/'
model = UserProfile
form_class = UserProfileForm
template_name = 'users/userprofile_edit_form.html'
def get_object(self):
return User.objects.get(username=self.request.user.username)
def get_success_url(self):
userid = self.kwargs['pk']
return reverse_lazy('users:user_profile',kwargs={'pk': userid})
网址:
from django.conf.urls import url
from users import views
app_name = 'users'
urlpatterns = [
url(r'^$',views.UserListView.as_view(),name='user_list'),
url(r'^(?P<pk>\d+)/$',views.detailprofile,name='user_profile'),
# url(r'^(?P<pk>\d+)/edit$',views.UserEditProfileView.as_view(),name='user_profile_edit'),
url(
regex=r'^profile/$',
view=views.UserEditProfileView.as_view(),
name='profile'
),
]
更新2
用户配置文件_详细信息.html模板:
{% extends "base.html" %}
{% block content %}
<div class="sidebar-userinfo">
{% if user.userprofile.profile_pic %}
<img class="profile-pic" src="{{ user.userprofile.profile_pic.url }}">
{% else %}
<img class="profile-pic" src="../../../media/profile_pics/default_pro_pic.png">
{%endif%}
<div class="profile-top-info">
<h2 class="profile-name">{{ user.userprofile.first_name }}</h2>
{% if user.userprofile.location %}
<p class="profile-info small-text">{{ user.userprofile.location }}</p>
{% endif %}
</div>
<div class="profile-info-group">
<p class="accent list-text">Title:</p>
<p class="profile-info list-text">{{ user.userprofile.title }}</p>
<p class="accent list-text">Website:</p>
<p class="profile-info list-text">{{ user.userprofile.website }}</p>
<p class="accent list-text">I'm a:</p>
{% if user.userprofile.user_type == '1' %}
<p class="profile-info list-text">Designer</p>
{% elif user.userprofile.user_type == '2' %}
<p class="profile-info list-text">Developer</p>
{% else %}
<p class="profile-info list-text">Both</p>
{% endif %}
{% if user.userprofile.about %}
<p class="accent list-text">About Me:</p>
<p class="profile-info list-text">{{ user.userprofile.about }}</p>
{% endif %}
<p class="accent list-text">Member Since:</p>
<p class="profile-info list-text">{{ user.userprofile.join_date }}</p>
{% if user.userprofile.twitter %}
<p class="accent list-text">Twitter:</p>
<p class="profile-info list-text">{{ user.userprofile.twitter }}</p>
{% endif %}
{% if user.userprofile.dribbble %}
<p class="accent list-text">Dribbble:</p>
<p class="profile-info list-text">{{ user.userprofile.dribbble }}</p>
{% endif %}
{% if user.userprofile.github %}
<p class="accent list-text">Git Hub:</p>
<p class="profile-info list-text">{{ user.userprofile.github }}</p>
{% endif %}
</div>
{% if request.user.is_authenticated %}
<a class="link" href="{% url 'users:user_profile_edit' %}">Edit Profile</a>
{% endif %}
</div>
<div class="content-right">
{% if user.is_authenticated %}
<a class="btn float-right" href="{% url 'feed:new_post' %}">New Post</a>
{% endif %}
{% include 'feed/userpost_list_inner.html' %}
</div>
{% endblock %}
用户配置文件\u编辑_表单.html模板:
{% extends "users/base.html" %}
{% block content %}
<div class="form-title">
<h2 class="form-title-text">Edit Profile</h2>
</div>
<div class="user-forms-base">
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<input class="btn" type="submit" value="Save" />
</form>
</div>
{% endblock %}
我会用稍微不同的方法。你真的不希望那些url存在,因为你不想诱使用户改变号码。相反,我根本不会将PK传递到URL中,而是通过覆盖
get_object
方法来获取当前用户以使用request.user.id
。你知道吗有关详细信息,请参见Here
让我知道澄清是否有帮助
你可以用一个装饰工来做这个。`你知道吗
只要使用装饰器,它就是这么简单。你知道吗
Sam Bobel是对的,下面是配置文件问题的代码解决方案:
forms.py
:urls.py
:views.py
:相关问题 更多 >
编程相关推荐