Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python继承和Django_Django_Python 3.x_Inheritance - Fatal编程技术网

Python继承和Django

Python继承和Django,django,python-3.x,inheritance,Django,Python 3.x,Inheritance,我只是第一次在Django应用程序中尝试继承,我遇到了麻烦 我有两个类,ProjectDetailView和ProjectDetailView,它们带有从ProjectDetailView继承的链接。我基本上希望从基类(init然后buildContextData)运行一组函数。在此之后,我想使用一个名为uu buildPrevAndNext的函数添加到上下文数据,然后完成get函数。我附上了一些代码。__init似乎工作正常,但当我使用super()时,_buildContextData会一直

我只是第一次在Django应用程序中尝试继承,我遇到了麻烦

我有两个类,ProjectDetailView和ProjectDetailView,它们带有从ProjectDetailView继承的链接。我基本上希望从基类(init然后buildContextData)运行一组函数。在此之后,我想使用一个名为uu buildPrevAndNext的函数添加到上下文数据,然后完成get函数。我附上了一些代码。__init似乎工作正常,但当我使用super()时,_buildContextData会一直出错,不会进入基类:

class ProjectDetailView(View):

    def __init__(self):
        self.__contextData = None
        self.__intPK = None
        self.__intLangID = None

    def __buildContextData(self, request,  **kwargs):
        '''
        This is used to build the context data that we are going to send to the template.
        I have done it this way because I want to build two versions of the class.   One
        that has a previous/next project link, but another class that does not.   This base
        class does not have the previous/next link.
        '''

        # Get the primary key of the Project and the corresponding MyProject object
        self.__intPK = self.kwargs['pk']
        project = MyProject.objects.get(pk=self.__intPK)

        # get the formatted date
        formatedDate = project.date.strftime("%d-%b-%Y")

        # Either throw a 404 or get the group id of the training course
        if not project.language:
            raise Http404

        # Store the language id
        self.__intLangID = project.language.id

        # Build the contextData
        self.__contextData = {
            'project': project,
            'formatedDate': formatedDate,
            'language': project.language,
        }


    def get(self, request, **kwargs):

        # Build the context data
        self.__buildContextData(request, **kwargs)

        # Now send the data to the template
        return render(request, 'project_detail.html', context=self.__contextData)

class ProjectDetailViewWithLinks(ProjectDetailView):

    def __init__(self):
        super().__init__()

    def __buildContextData(self, request,  **kwargs):
        super().__buildContextData(request, **kwargs)

    def __buildPrevAndNext(self, request):
        '''
        This should be run after __buildContextData and adds the extra data into the context for displaying
        the previous and next links.
        '''

        # Filter TrainingCourse table to a list containing the same group id of this course
        filtProjects = MyProject.objects.filter(language_id=self.__intLangID)

        # Get the next course from this list and the previous course from this list
        nextProject = filtProjects.filter(pk__gt=self.__intPK)
        if len(nextProject)>0:
            nextProject = nextProject[0]

        prevProject = filtProjects.filter(pk__lt=self.__intPK)
        if len(prevProject)>0:
            prevProject = prevProject[len(prevProject)-1]

        # Add the previous and next to the context
        self.__contextData['next'] = nextProject
        self.__contextData['prev'] = prevProject


    def get(self, request, **kwargs):

        # First, build the context data in a similar manner to the ProjectDetailView class
        self.__buildContextData(request, **kwargs)

        # Next, add the next and prev data onto the context data
        self.__buildPrevAndNext(request)

        # Finally render the template
        return render(request, 'project_detail.html', context=self.__contextData)
请你解释一下我做错了什么,甚至没有涉及基类函数

谢谢


标记

重命名方法,使其具有单个前导下划线:

def _buildContextData(self, request,  **kwargs):

双前导下划线触发器。

谢谢。我想一种想法是,我将函数和变量设置为私有的,而不是受保护的。这是一个愚蠢的错误,但谢谢你的帮助。