我使用django-neomodel集成创建了一个简单的Django Webapp。尝试创建新书时,请在“ http://localhost:8000/book/new”处提交后,看到回溯中显示的错误。
我在网上搜索过,在大多数情况下,该错误似乎是由于在指定模型名称时存在某些错字,或者是由于无意中使用了字符串。由于这些原因,我进行了仔细检查,但在我的代码中看不到任何此类问题。
此外,我尝试使用窗体,方法是创建窗体并在视图中提供指定的窗体,而不是模型本身。但在这种情况下,我也看到相同的错误。
models.py:
TITLE_MAX_LEN = 100
USERNAME_MAX_LEN = 25
NAME_MAX_LEN = 25
class Book(DjangoNode):
custom_pk = UniqueIdProperty()
title = StringProperty(max_length=TITLE_MAX_LEN, unique_index=True, required=True)
description = StringProperty()
difficulty = IntegerProperty()
importance = FloatProperty()
class Meta:
app_label = 'knowledge'
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('book-detail', kwargs={'pk': self.custom_pk})
views.py
class BookCreateView(CreateView):
model = Book
fields = ['title', 'description']
template_name = "knowledge/book_form.html"
class BookDetailView(DetailView):
model = Book
template_name = "knowledge/book_detail.html"
urls.py
urlpatterns = [
path('book/new/', BookCreateView.as_view(), name='book-create'),
path('book/<str:pk>/', BookDetailView.as_view(), name='book-detail'),
]
跟踪:
Traceback:
File "/Users/sam/code/website/django_env/lib/python3.7/site-packages/django/core/handlers/exception.py" in inner
34. response = get_response(request)
File "/Users/sam/code/website/django_env/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response
115. response = self.process_exception_by_middleware(e, request)
File "/Users/sam/code/website/django_env/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response
113. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/sam/code/website/django_env/lib/python3.7/site-packages/django/views/generic/base.py" in view
71. return self.dispatch(request, *args, **kwargs)
File "/Users/sam/code/website/django_env/lib/python3.7/site-packages/django/views/generic/base.py" in dispatch
97. return handler(request, *args, **kwargs)
File "/Users/sam/code/website/django_env/lib/python3.7/site-packages/django/views/generic/detail.py" in get
106. self.object = self.get_object()
File "/Users/sam/code/website/django_env/lib/python3.7/site-packages/django/views/generic/detail.py" in get_object
30. queryset = self.get_queryset()
File "/Users/sam/code/website/django_env/lib/python3.7/site-packages/django/views/generic/detail.py" in get_queryset
67. return self.model._default_manager.all()
Exception Type: AttributeError at /book/63038803d06f4270b1bf4a738eefe916/
Exception Value: type object 'Book' has no attribute '_default_manager'
Django文档说Django负责创建默认管理器。不知道为什么我的代码会引发此错误。 任何提示表示赞赏。请帮忙。谢谢!