Django:名称错误:名称“类别”没有定义
我正在练习 Django 并使用一些在线教程来构建一个网络博客。第一个项目进展顺利,但是当我尝试第二个项目时,通过开发第一个视图,出现了这样的语句:
categories = models.ManyToManyField(Category, related_name ="packages")
在教程中,验证模型给出了 0 个错误,但是当我运行验证命令时它给出了我:名称错误:名称“类别”未定义
我三次检查了所有文件的语法,没有任何语法错误,教程中没有提到其他导入。
怎么了?
I'm practicing on Django and using some online tutorial to build a web blog. It went smoothly with the first project, yet when I tried the 2nd one, through developing the first view, there was this statement:
categories = models.ManyToManyField(Category, related_name ="packages")
In the tutorial, validating the model gives 0 errors, yet when I ran validating command it gave me :NameError: name 'Category' is not defined
I triple checked the syntax of all the file and there was no single syntax error, there is no additional imports mentioned in the tutorial.
What's wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
听起来你可能是 Python 新手,因为你说“我三次检查了所有文件的语法,没有任何语法错误,教程中没有提到额外的导入。”
请注意,在 Python 中,许多在 C++ 等语言中在编译时捕获的与名称相关的错误会在运行时捕获。当我开始使用 Python 时,这让我有点困惑。运行时错误可能只是表明仅仅是拼写错误等(如果这听起来很糟糕,请不要担心 - 有免费的工具可以检查您的程序作为编译器是否有像其他语言那样的东西)
是否在该行出现在同一个文件中?
如果没有,您必须专门导入名称类别。假设 Category 是在另一个文件 argh.py 中定义的。
不行。
您需要这样做
(或者更改您的代码以引用 argh.Category)
无论如何,除非您提供更多信息,否则您的问题实际上无法得到更好的回答。那条线不是问题。问题可能出在类别定义上。类别的定义在哪里? Category 是另一个模型类。应该有类似的代码
,并且它应该位于同一个文件中,或者如果它位于名为“prison.py”的文件中,那么您正在处理的文件应该包含
It sounds like you may be new to Python, since you say "I triple checked the syntax of all the file and there was no single syntax error, there is no additional imports mentioned in the tutorial."
Be aware that in Python, many name-related errors that would be caught at compile time in languages like C++ are caught at runtime instead. This tripped me up a little when I started using Python. Runtime errors could simply be indicating a mere typo, etc. (If this sounds nasty, don't worry-- there's free tools out there to check your program as a compiler for stuff like that some other languages would)
Is Category defined in the same file that that line appears in?
If not, you must specifically import the name Category. Suppose Category is defined in another file, argh.py.
is no good.
You would need to do
(or alternatively change your code to reference argh.Category)
Anyway, your question can't really be answered better than that unless you give more information. That line isn't the problem. The problem is probably with the Category definition. Where is the definition of Category? Category is another model class. There should be code like
And it should either be in the same file, or if it's in a file called "prison.py" then the file you are working on should contain
链接到“某些”会很有用。
但是,问题是您尚未导入类别或您使用提到的键在模型下定义类别。
It would be useful to link on "some".
However, problem is that You have not imported Category or You define Category under the model with the key mentioned.
我得到了同样的错误如下:
因为我将
Category
设置为 ForeignKey() 在Category
类之前定义,如下所示:因此,我将
Category
设置为ForeignKey() 在
Category
类定义如下,然后错误得到解决:或者,我将带引号的
Category
设置为ForeignKey()
作为如下所示,则错误已解决:*您可以看到 我的问题和答案解释了 Django 模型字段中带或不带引号的类名具有外键关系。
I got the same error below:
Because I set
Category
to ForeignKey() beforeCategory
class is defined as shown below:So, I set
Category
toForeignKey()
afterCategory
class is defined as shown below, then the error was solved:Or, I set
Category
with quotes toForeignKey()
as shown below, then the error was solved:*You can see my question and the answer explaining the class name with or without quotes in a Django model field to have foreign key relationship.