views:

30

answers:

2

Hi,

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 ?

A: 

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.

Almad
A: 

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.

import argh

is no good.

You would need to do

from argh import Category

(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

class Category:
     #....

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

from prison import Category
Domingo Galdos
Thanks both, Not new but in fact I missed something in the code where I should define the class Category, I was following a tutorial and I thought that Category is included within the tutorial package, defined the class and the problem solved.Thanks
ma2moun
So, is your problem solved? Did you find these answers helpful?
Domingo Galdos