这是一个令我疯狂的相对导入错误。 以下是目录树和代码:
books
├── admin.py
├── apps.py
├── forms.py
├── __init__.py
├── models.py
├── tests.py
├── urls.py
└── views.py
models.py
from django.db import models from django.core.urlresolvers import reverse
from . import views
class BookList(models.Model):
cover = models.ImageField(upload_to='cover/%Y/%m/%d')
# cover = models.CharField(max_length=200, blank=False)
title = models.CharField(max_length=100, blank=False)
author = models.CharField(max_length=50)
publisher = models.CharField(max_length=50)
review = models.TextField()
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse(views.detail_book, kwargs={'id': self.id})
views.py(代码在pdb.set_trace()下面的行崩溃了)
import pdb
from django.views import generic
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
pdb.set_trace()
from .models import BookList
from .forms import BookListForm
class BookRecommend(generic.ListView):
model = BookList
template_name = 'books/books_list.html'
context_object_name = 'books'
paginate_by = 1
def add_book(request):
if request.method == 'POST':
form = BookListForm(request.POST)
if form.is_valid():
book = form.save()
return HttpResponseRedirect(reverse(detail_book, kwargs={id: book.id}))
else:
form = BookListForm()
return render(request, 'books/add_book.html', {'form': form})
def delete_book(request):
pass
def modify_book(request):
pass
def detail_book(request, id=1):
book = get_object_or_404(BookList, id=id)
return render(request, 'books/detail_book.html', {'book': book})
追回:
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0xb621a2fc>
Traceback (most recent call last):
File "/home/michael/Envs/DJ19/lib/python3.5/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/home/michael/Envs/DJ19/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
autoreload.raise_last_exception()
File "/home/michael/Envs/DJ19/lib/python3.5/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
six.reraise(*_exception)
File "/home/michael/Envs/DJ19/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/home/michael/Envs/DJ19/lib/python3.5/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/home/michael/Envs/DJ19/lib/python3.5/site-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/michael/Envs/DJ19/lib/python3.5/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models(all_models)
File "/home/michael/Envs/DJ19/lib/python3.5/site-packages/django/apps/config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "/home/michael/Envs/DJ19/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 665, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/home/michael/Envs/DJ19/Profile/books/models.py", line 4, in <module>
from . import views
File "/home/michael/Envs/DJ19/Profile/books/views.py", line 9, in <module>
from .models import BookList
ImportError: cannot import name 'BookList'
在添加一些视图功能之前,先前版本没有任何障碍。 提前谢谢!