在Django中有一个意外的关键字参数

时间:2019-05-24 13:01:33

标签: python django python-3.x

我正在使用recommended函数,该函数通过此帖子中的相同标签生成帖子查询集。 它的工作原理几乎是这样的: 打开帖子页面时,PostDetailView会将参数post_pk传递给Sidebar.get_recommend,然后传递给Post.recommend_posts

然后我收到此错误

TypeError: recommend_posts() got an unexpected keyword argument 'post_pk'
class Post(models.Model):
...
    @classmethod
    def recommend_posts(cls, post_pk=None):
        post = cls.queryset.get(pk=post_pk)
        post_tags = [tag for tag in post.tag.all()]
        recommend_list = Post.objects.none()
        for tag in post_tags:
            recommend_list = recommend_list | tag.post_set.all()
        recommend_list = recommend_list.distinct().exclude(pk=post_pk)
        return recommend_list

class PostDetailView(CommonViewMixin, DetailView):

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        post_pk = self.kwargs.get(self.pk_url_kwarg)
        context.update({'sidebars':Sidebar.get_recommend(post_pk=post_pk),})
        return context
class Sidebar(models.Model):
...
    @property
    def content_html(self):
        result = ''
        ...
        ...
        elif self.display_type == self.DISPLAY_RECOMMEND:
            context = {
                'posts': Post.recommend_posts(self.post_pk)[:5]
            }
            result = render_to_string('config/blocks/sidebar_comments.html', context)

        return result

    @classmethod
    def get_recommend(cls, post_pk=None):
        cls.post_pk = post_pk
        return cls.objects.filter(status=cls.STATUS_SHOW).filter(display_type=cls.DISPLAY_RECOMMEND)
Traceback (most recent call last):
  File "/home/mark/code/env-typeidea/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/mark/code/env-typeidea/lib/python3.6/site-packages/django/core/handlers/base.py", line 156, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/mark/code/env-typeidea/lib/python3.6/site-packages/django/core/handlers/base.py", line 154, in _get_response
    response = response.render()
  File "/home/mark/code/env-typeidea/lib/python3.6/site-packages/django/template/response.py", line 106, in render
    self.content = self.rendered_content
  File "/home/mark/code/env-typeidea/lib/python3.6/site-packages/django/template/response.py", line 83, in rendered_content
    content = template.render(context, self._request)
  File "/home/mark/code/env-typeidea/lib/python3.6/site-packages/django/template/backends/django.py", line 61, in render
    return self.template.render(context)
  File "/home/mark/code/env-typeidea/lib/python3.6/site-packages/django/template/base.py", line 171, in render
    return self._render(context)
  File "/home/mark/code/env-typeidea/lib/python3.6/site-packages/django/test/utils.py", line 96, in instrumented_test_render
    return self.nodelist.render(context)
  File "/home/mark/code/env-typeidea/lib/python3.6/site-packages/django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "/home/mark/code/env-typeidea/lib/python3.6/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/home/mark/code/env-typeidea/lib/python3.6/site-packages/django/template/loader_tags.py", line 150, in render
    return compiled_parent._render(context)
  File "/home/mark/code/env-typeidea/lib/python3.6/site-packages/django/test/utils.py", line 96, in instrumented_test_render
    return self.nodelist.render(context)
  File "/home/mark/code/env-typeidea/lib/python3.6/site-packages/django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "/home/mark/code/env-typeidea/lib/python3.6/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/home/mark/code/env-typeidea/lib/python3.6/site-packages/django/template/defaulttags.py", line 209, in render
    nodelist.append(node.render_annotated(context))
  File "/home/mark/code/env-typeidea/lib/python3.6/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/home/mark/code/env-typeidea/lib/python3.6/site-packages/django/template/base.py", line 987, in render
    output = self.filter_expression.resolve(context)
  File "/home/mark/code/env-typeidea/lib/python3.6/site-packages/django/template/base.py", line 671, in resolve
    obj = self.var.resolve(context)
  File "/home/mark/code/env-typeidea/lib/python3.6/site-packages/django/template/base.py", line 796, in resolve
    value = self._resolve_lookup(context)
  File "/home/mark/code/env-typeidea/lib/python3.6/site-packages/django/template/base.py", line 837, in _resolve_lookup
    current = getattr(current, bit)
  File "/home/mark/code/env-typeidea/typeidea/typeidea/config/models.py", line 81, in content_html
    'posts': Post.recommend_posts(self.post_pk)[:5]
TypeError: recommend_posts() got an unexpected keyword argument 'post_pk'

0 个答案:

没有答案