[英]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.