大家!在用户详细信息页面上,我有用户的帖子,我想分页(稍后计划进行无限分页)。试图做到这一点,我得到错误“ UserDetailView”对象没有属性“ object_list”。我在寻找很多解决方案,但是没有找到清晰的示例如何在详细视图中对相关对象进行分页。对此表示赞赏,对此有任何帮助。
这是我给用户的DetailView:
class UserDetailView(DetailView,MultipleObjectMixin):
model = CustomUser
template_name = 'users/user_detail.html'
context_object_name = "user"
paginate_by = 5
def get_object(self):
id_ = self.kwargs.get('id')
return get_object_or_404(CustomUser, id=id_)
def get_context_data(self, **kwargs):
posts = self.object.posts_set.all().order_by('-created_at')
#context['posts'] = self.object.posts_set.all().order_by('-created_at')
context = super(UserDetailView, self).get_context_data(posts=posts, **kwargs)
context['main'] = Main.objects.get(pk=1)
context['supporters'] = CustomUser.objects.filter(team = self.object.team)
return context
我得到的错误是: 'UserDetailView'对象没有属性'object_list'
跟踪
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/user/1/
Django Version: 3.0.7
Python Version: 3.8.3
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'users.apps.UsersConfig',
'team.apps.TeamConfig',
'posts.apps.PostsConfig',
'main.apps.MainConfig',
'crispy_forms',
'django.contrib.humanize',
'rest_framework',
'pytils']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback (most recent call last):
File "C:\Users\User\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\User\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\User\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\User\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\views\generic\base.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Users\User\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\views\generic\base.py", line 97, in dispatch
return handler(request, *args, **kwargs)
File "C:\Users\User\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\views\generic\detail.py", line 107, in get
context = self.get_context_data(object=self.object)
File "C:\Users\User\Desktop\fansite\fantribune\users\views.py", line 66, in get_context_data
context = super(UserDetailView, self).get_context_data(posts=posts, **kwargs)
File "C:\Users\User\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\views\generic\detail.py", line 100, in get_context_data
return super().get_context_data(**context)
File "C:\Users\User\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\views\generic\list.py", line 115, in get_context_data
queryset = object_list if object_list is not None else self.object_list
Exception Type: AttributeError at /user/1/
Exception Value: 'UserDetailView' object has no attribute 'object_list'
答案 0 :(得分:0)
我解决了这个问题。下面是代码。对其他人可能有用。
class UserDetailView(DetailView,MultipleObjectMixin):
model = CustomUser
template_name = 'users/user_detail.html'
context_object_name = "user"
paginate_by = 5
def get_object(self):
id_ = self.kwargs.get('id')
return get_object_or_404(CustomUser, id=id_)
def get_context_data(self, **kwargs):
object_list = Posts.objects.filter(author=self.get_object())
#posts = self.object.posts_set.all().order_by('-created_at')
context = super(UserDetailView, self).get_context_data(object_list=object_list,**kwargs)
#context['posts'] = self.object.posts_set.all().order_by('-created_at')
context['main'] = Main.objects.get(pk=1)
context['supporters'] = CustomUser.objects.filter(team = self.object.team)
return context
因此,为了在DetailView中对相关对象进行分页,我添加了MultipleObjectMixin作为参数:
class UserDetailView(DetailView,MultipleObjectMixin):
然后在get_context_data中获取相关模型并定义上下文超级:
def get_context_data(self, **kwargs):
object_list = Posts.objects.filter(author=self.get_object())
context = super(UserDetailView, self).get_context_data(object_list=object_list,**kwargs)
#other code
和
答案 1 :(得分:0)
这看起来更像一个列表视图,您还可以在其中添加ValueError: String length does not equal format and resolution size
的数据:
CustomUser