简体   繁体   English

AttributeError: 'function' object 在 urls.py 中没有属性 'as_view'

[英]AttributeError: 'function' object has no attribute 'as_view',in urls.py

I written this logic in my views.py, and I used class based views, Detail view:我在views.py中写了这个逻辑,我使用了基于class的视图,详细视图:

@login_required
class profileView(DetailView):
    model = profile
    template_name = "users/profile.html"

and in urls.py file I've written this:在 urls.py 文件中我写了这个:

from django.urls import path,include
from . import views
from .views import profileView

urlpatterns = [
    path('register/',views.register,name="register"),
    path('login/',views.login_user,name="login_user"),
    path('profile/',profileView.as_view(),name="profile_view"),
]

the django version that I'm using is 3.1 and python version is 3.8.我使用的 django 版本是 3.1,python 版本是 3.8。

I hope that someone has an answer to my question.我希望有人能回答我的问题。

You can not make use of @login_required for a class-based view, since that returns a function.您不能将@login_required用于基于类的视图,因为它会返回 function。 You use the LoginRequiredMixin [Django-doc] :您使用LoginRequiredMixin [Django-doc]

from django.contrib.auth.mixins import LoginRequiredMixin

class profileView(LoginRequiredMixin, DetailView):
    model = profile
    template_name = 'users/profile.html'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django:AttributeError:'function'对象在urls.py中没有显示属性'as_view' - Django: AttributeError: 'function' object has no attribute 'as_view' showing in urls.py Django AttributeError: 'function' object has no attribute 'as_view' in urls.py - Django AttributeError: 'function' object has no attribute 'as_view' in urls.py AttributeError:“函数”对象没有属性“as_view” - AttributeError: 'function' object has no attribute 'as_view' Django“ AttributeError:'function'对象没有属性'as_view'” - Django “AttributeError: 'function' object has no attribute 'as_view'” AttributeError: 'function' object 没有属性 'as_view'。 怎么了? - AttributeError: 'function' object has no attribute 'as_view'. What's wrong? “函数”对象没有属性“as_view” - 'function' object has no attribute 'as_view' AttributeError:类型对象“ DirectView”没有属性“ as_view” - AttributeError: type object 'DirectView' has no attribute 'as_view' AttributeError:类Home没有属性'as_view' - AttributeError: class Home has no attribute 'as_view' TypeError:“函数”对象没有来自urls.py的属性“ __getitem__” - TypeError: 'function' object has no attribute '__getitem__' from urls.py 为什么显示此错误“功能”对象没有属性“ as_view” - Why show this Error 'function' object has no attribute 'as_view'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM