python - 如何在 Django 中的更新/创建/删除 View 上使用 login_required

标签 python django authentication authorization

我正在尝试使用@login_reuqired装饰器。对于正常功能,它工作正常,但是当我尝试在 UpdateView 中使用它时,我收到此错误 “AttributeError:'function'对象没有属性'as_view'”

这是我的view.py

@login_required
class RoomUpdate(UpdateView):
    model = Room
    fields = ['Name', 'RoomTypeID']
    template_name='WebApp/room_form.html'`

这是在我的urls.py

 url(r'^roomList/updateRoom/(?P<pk>[0-9]+)/$', views.RoomUpdate.as_view(), name='room_update'),

有什么建议吗?

最佳答案

对于 Django 1.9+,您可以使用 mixin:

from django.contrib.auth.mixins import LoginRequiredMixin

class RoomUpdate(LoginRequiredMixin, UpdateView):
    fields = ['Name', 'RoomTypeID']
    template_name='WebApp/room_form.html'`

关于python - 如何在 Django 中的更新/创建/删除 View 上使用 login_required,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44935522/

相关文章:

Django : when client request http methods that are not defined in the view class

python - 开始 MySQL/Python

python - 奇怪的 OpenCV .read() 挂起没有错误

javascript - Web 抓取需要使用/Javascript 警报进行身份验证的内部站点

php - 如何在 PHP/MySQL 中对用户进行身份验证?

java - Apache HttpAsyncClient

python - 如何将html输入到Flask?

python - Django-从子查询中注释多个字段

python可以将图像文件读取为二进制文件

python - Pandas - 将 numpy 数组存储在数据框列中,这是函数的结果