[英]ImportError: attempted relative import beyond top-level package django
I'm writing a site on Django and faced the problem that when relative importing from a directory located in the same directory as the directory in which the import is carried out, the following error appears:我在 Django 上写了一个站点,并遇到了从与执行导入的目录位于同一目录中的目录进行相对导入时,出现以下错误的问题:
ImportError: attempted relative import beyond top-level package
here is my import,which is in the file online_book/shop-cart/cart.py这是我的导入,位于文件online_book/shop-cart/cart.py
from ..books.models import Book
the Book model is in the file online-book/books/models.py这本书 model 在文件online-book/books/models.py 中
help me find solution to this problem please请帮我找到解决这个问题的方法
imports are relative to where the manage.py
is导入相对于manage.py
所在的位置
so your import should be like this:所以你的导入应该是这样的:
from books.models import Book
Set dir online_book as root directory in settings在设置中将 dir online_book 设置为根目录
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.