Skip to content
Advertisement

In Django, how do I get the corresponding key for a value passed in through the url?

Here is my models.py:

JavaScript

Here is from my urls.py:

JavaScript

Here is the function from views.py:

JavaScript

I want to be able to have the url look like this: https://www.mywebsite.com/ballers, but when I try to run it, I get this error: Field ‘show’ expected a number but got ‘ballers’. I know I can fix this by calling the url https://www.mywebsite.com/0, but I don’t want the url to look like that.

Advertisement

Answer

In Django it takes first argument as the value to pass into db (field), so basically with:

JavaScript

it tries to look directly with passing value. DB-wise it knows ONLY integers for that matter. So instead of looking directly, try to make small change:

JavaScript

and also move choices inside the model:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement