作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在通过“Django 3 by example”这本书学习 django,现在我正在尝试构建一个电子学习平台。但是,我的 views.py 文件中出现了一个奇怪的错误。
这是我的意见文件:
from django.views.generic.list import ListView
from django.urls import reverse_lazy
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMixin
from .models import Course
class ManageCourseListView(ListView):
model = Course
template_name = 'educa/manage/educa/list.html'
permission_required = 'educa.view_course'
def get_queryset(self):
qs = super().get_queryset()
return qs.filter(owner=self.request.user)
class OwnerMixin(object):
def get_queryset(self):
qs = super().get_queryset()
return qs.filter(owner=self.request.owner)
class OwnerEditMixin(object):
def form_valid(self, form):
form.instance.onwer = self.request.user
return super().form_valid(form)
class OwnerCourseMixin(object, LoginRequiredMixin, PermissionRequiredMixin):
model = Course
fields = ['subject', 'title', 'slug', 'overview']
success_url = reverse_lazy('manage_course_list')
class OwnerCourseEditMixin(OwnerCourseMixin, OwnerEditMixin):
template_name = 'educa/manage/course/list.html'
class CourseCreateView(OwnerEditMixin, CreateView):
permission_required = 'educa.add_course'
class CourseUpdateView(OwnerCourseEditMixin, UpdateView):
permission_required = 'educa.change_course'
class CourseDeleteView(OwnerCourseMixin, DeleteView):
template_name = 'educa/manage/course/delete.html'
permission_required = 'educa.delete_course'
错误在第 30 行。
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\Users\Padma Jain\AppData\Local\Programs\Python\Python38\lib\threading.py", line 932, in _bootstrap_inner
self.run()
File "C:\Users\Padma Jain\AppData\Local\Programs\Python\Python38\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\Padma Jain\Desktop\django\educa\venv\lib\site-packages\django\utils\autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "C:\Users\Padma Jain\Desktop\django\educa\venv\lib\site-packages\django\core\management\commands\runserver.py", line 118, in inner_run
self.check(display_num_errors=True)
File "C:\Users\Padma Jain\Desktop\django\educa\venv\lib\site-packages\django\core\management\base.py", line 392, in check
all_issues = checks.run_checks(
File "C:\Users\Padma Jain\Desktop\django\educa\venv\lib\site-packages\django\core\checks\registry.py", line 70, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "C:\Users\Padma Jain\Desktop\django\educa\venv\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "C:\Users\Padma Jain\Desktop\django\educa\venv\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
return check_method()
File "C:\Users\Padma Jain\Desktop\django\educa\venv\lib\site-packages\django\urls\resolvers.py", line 408, in check
for pattern in self.url_patterns:
File "C:\Users\Padma Jain\Desktop\django\educa\venv\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\Padma Jain\Desktop\django\educa\venv\lib\site-packages\django\urls\resolvers.py", line 589, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Users\Padma Jain\Desktop\django\educa\venv\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\Padma Jain\Desktop\django\educa\venv\lib\site-packages\django\urls\resolvers.py", line 582, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Users\Padma Jain\AppData\Local\Programs\Python\Python38\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\Users\Padma Jain\Desktop\django\educa\elearn\elearn\urls.py", line 24, in <module>
path('course/',include('educa.urls')),
File "C:\Users\Padma Jain\Desktop\django\educa\venv\lib\site-packages\django\urls\conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "C:\Users\Padma Jain\AppData\Local\Programs\Python\Python38\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\Users\Padma Jain\Desktop\django\educa\elearn\educa\urls.py", line 2, in <module>
from .views import *
File "C:\Users\Padma Jain\Desktop\django\educa\elearn\educa\views.py", line 30, in <module>
class OwnerCourseMixin(object, LoginRequiredMixin, PermissionRequiredMixin):
TypeError: Cannot create a consistent method resolution
order (MRO) for bases object, LoginRequiredMixin, PermissionRequiredMixin
如果需要,这里是 educa 应用程序的 urls.py 文件:
from django.urls import path
from .views import *
urlpatterns = [
path('mine/',ManageCourseListView.as_view(),name='manage_course_list'),
path('create/',CourseCreateView.as_view(),name='course_create'),
path('<pk>/edit/',CourseUpdateView.as_view(),name='course_edit'),
path('<pk>/delete/',CourseDeleteView.as_view(),name='course_delete'),
]
这是我第一次构建这么大的应用程序。有人可以告诉我哪里错了吗?
class OwnerCourseMixin(object, LoginRequiredMixin, PermissionRequiredMixin):
....
对此:
class OwnerCourseMixin(OwnerMixin, LoginRequiredMixin, PermissionRequiredMixin):
....
最佳答案
在 Python 中,每个类都继承自一个名为 object
的内置基本类。 .
您的 OwnerCourseMixin
继承自 object
和 其他一些类(class)。因为其他类,在这种情况下,LoginRequiredMixin
和 PermissionRequiredMixin
, 已经继承自 object
,Python 现在无法确定首先查找哪个类。
您不需要从 object
继承这里。
class OwnerCourseMixin(LoginRequiredMixin, PermissionRequiredMixin):
model = Course
fields = ['subject', 'title', 'slug', 'overview']
success_url = reverse_lazy('manage_course_list')
那应该工作。
关于python - 类型错误 : Cannot create a consistent method resolution in django,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64602038/
我是一名优秀的程序员,十分优秀!