Skip to content
Advertisement

django – how to save api data into model from template button

I am using the Yelp API to search bars in a any location. This uses a template called results.html that has a button. Clicking this button should save the Yelp API unique ID of a Bar into my models. In my results dictionary that I loop over I call this ‘id’. This is where I am having issues, taking the id from the template, saving it in the add_list views to the model BarList. This will not save to my database, I believe the main problem is my logic in add_list.

I get the following error. The long string is the id from the loop. Results.id.

JavaScript

Views.py

JavaScript

Models.py

JavaScript

Urls.py

JavaScript

results.html template

JavaScript

Advertisement

Answer

Few things I needed to do. The not null constraint error is fixed by adding a constraint to the model. Furthermore I needed to finish writing my view to properly save.

Updated Models

JavaScript

Updated View

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