python - url 中没有 pk 的 Django UpdateView

标签 python django security url

我正在使用 Django 的 UpdateView 来更新用户的个人资料。 我在 url 中传递配置文件的 pk,如下所示:

url(r'^profile/edit/(?P<pk>(\d+))$', profileviews.ProfileUpdateView.as_view(),

在 view.py 中,我只使用 UpdateView:

class ProfileUpdateView(UpdateView):
    model = Profile
    form_class = UserProfileForm

它可以工作。 但是,我发现如果我以另一个用户身份登录并输入相同的 url,我将能够编辑该用户的个人资料! 这绝对是错误的,其他用户绝不应该有权编辑其他人的个人资料。

请问有没有很好的方案可以解决这个问题?在 url 中隐藏 pk?或者其他更好的解决方案?

非常感谢。

最佳答案

网址

# ... omitted
    url(r'^profile/edit/$', profileviews.ProfileUpdateView.as_view(),
# ... omitted

观看次数

class ProfileUpdateView(UpdateView):
    model = Profile
    form_class = UserProfileForm

    def get_object(self):
        """
        Returns the request's user.
        """
        return self.request.user.get_profile()

    # Then (unrelated, but for security)
    dispatch = login_required(UpdateView.dispatch)

关于python - url 中没有 pk 的 Django UpdateView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20975430/

相关文章:

python - Django 在重定向时找不到模式名称

python - django 完整性错误,我知道为什么,但不知道如何解决

google-chrome - "Secure origin"不是HTTPS吗?

python - Django、html、css - 页脚样式适用于所有正文

python - 无法加载在 Google Colaboratory 中创建的模型

python - 合并 DataFrame 中的重复列

javascript - 如何在网页中安全地使用 "eval"用户代码?

python - 使用 python 和 selenium 进行网络爬行

django - 如何将链接放入 Django 错误消息中

security - 阻止对单个 mercurial 存储库中特定分支的写访问