when i am trying to implement class view it showing the below error Thanks in Advance views.py----
- from re import template import django from django.shortcuts import
render from django.views.generic import ListView from .models import
Post
def PostListView(ListView):
model = Post
template_name = 'blog/home.html'
context_object_name = 'posts'
ordering = ['-date_posted']
urls.py-----
- from django.urls import path from .views import PostListView from .
import views
urlpatterns = [
path('', PostListView.as_view(),name="blog-home"),
path('about/', views.about,name="blog-about"), ]
error------
- return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 850, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "/Users/kundan/Documents/CodeHub/Python Framework/DjangoWeb/blogspot/blog/urls.py", line 7, in <module>
path('', PostListView.as_view(),name="blog-home"),
AttributeError: 'function' object has no attribute 'as_view'
You are defining a function
, but you should define a class
.
def PostListView(ListView):
...
# change to:
class PostListView(ListView):
...
Remember, that classes
inherit another classes
in their ()
, but functions
recieve variables to use inside them. It is similar in some way, but very different in usage.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.