首页Django正文

不知道哪里代码有问题

提问已结 5 46
lyhabc
lyhabcDjango实战会员2018年8月5日 17:53

http://the5fireblog.b0.upaiyun.com/v_the5fire/16594a883b8a28f66c84042c0062d132.pnghttp://the5fireblog.b0.upaiyun.com/v_the5fire/9c50e2e96615a56ffbdc24121da09c92.pnghttp://the5fireblog.b0.upaiyun.com/v_the5fire/4694a19944e0e03d6325587f6b2ed16b.png 报错

class PostView(CommonMixin,DetailView):
    model = Post
    template_name = 'blog/detail.html'
    context_object_name = 'post'

正常


class PostView(DetailView):
    model = Post
    template_name = 'blog/detail.html'
    context_object_name = 'post'

postview函数里面不继承CommonMixin就正常,继承了CommonMixin就报这个typeerror的错误

调试了源代码,没看出什么错误

[05/Aug/2018 17:35:16] "GET / HTTP/1.1" 200 482
<class 'blog.models.Post'>
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Internal Server Error: /post/1/
class BaseDetailView(SingleObjectMixin, View):
    """
    A base view for displaying a single object
    """
    def get(self, request, *args, **kwargs):
        self.object = self.get_object()
        # import pdb;pdb.set_trace()
        print(type(self.object))
        print('x'*30)
        context = self.get_context_data(object=self.object)
        return self.render_to_response(context)


    def get_context_data(self, **kwargs):
        """
        Insert the single object into the context dict.
        """
        # import pdb;pdb.set_trace()
        context = {}
        if self.object:
            context['object'] = self.object
            context_object_name = self.get_context_object_name(self.object)
            if context_object_name:
                context[context_object_name] = self.object
        context.update(kwargs)
        return super(SingleObjectMixin, self).get_context_data(**context)
回帖
  • lyhabc
    lyhabc(楼主)
    2018年8月6日 09:42

    报错 class PostView(CommonMixin,DetailView): model = Post template_name = 'blog/detail.html' context_object_name = 'post'

    正常 class PostView(DetailView): model = Post template_name = 'blog/detail.html' context_object_name = 'post'

    postview函数里面不继承CommonMixin就正常,继承了CommonMixin就报这个typeerror的错误

    调试了源代码,没看出什么错误

  • lyhabc
    lyhabc(楼主)
    2018年8月6日 09:43

    报错

    class PostView(CommonMixin,DetailView): 
    model = Post 
    template_name = 'blog/detail.html' 
    context_object_name = 'post'
    

    正常

    class PostView(DetailView): 
    model = Post 
    template_name = 'blog/detail.html' 
    context_object_name = 'post'
    

    postview函数里面不继承CommonMixin就正常,继承了CommonMixin就报这个typeerror的错误

    调试了源代码,没看出什么错误

  • the5fire
    2018年8月6日 10:02
  • lyhabc
    lyhabc(楼主)
    2018年8月6日 11:07

    已经解决

  • JummyWu
    2018年9月30日 16:04

    {[微笑]}