Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
322 views
in Technique[技术] by (71.8m points)

python - Django - KeyError at /auctions/create 'category.' (New to Django, any help is appreciated!)

I am new to Django and creating a dropdown menu so that users can choose a category from the dropdown. This category will be saved on an entry on the website, when a user creates a new entry. I migrated the model, but I'm getting: KeyError at /auctions/create 'category.' Any help is really appreciated.

Views.py (Code for creating the dropdown menu for categories)

views.py

models.py

models.py

forms.py

forms.py

Here is the create part in views.py for creating a new entry. 'form' for CreateForm() is the model form for creating the entry. 'form1' for CategoryForm() is the model form for the category drop down list. The create function was working completely fine, until I added in the parts for the categories and category form.

create

urls.py for applicable functions (I have tried many variations for the category one but I'm still having trouble figuring out what it should be, I have tried str and int.) Sorry I'm a beginner but any help would be great!

path("auctions/create", views.create, name="create"),
path("listings/<int:id>", views.listingpage, name="listingpage"),
path("cat/<int:id>", views.cat, name="cat"),

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

A Python KeyError exception is what is raised when you try to access a key that isn’t in a dictionary,and I think your error is probably coming from here

categoryCreated = categories.objects.create(category=category)

and you should write your model name Categories for a good practice not categories


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...