title: django - how to save api data into model from template button
body: <p>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.</p>
  <p>I get the following error. The long string is the id from the loop. Results.id.</p>
  <pre><code>IntegrityError at /add/Jlhck4bGoXaXfuoMOKGRww/
  
  NOT NULL constraint failed: API_barlist.api_id
  </code></pre>
  <p>Views.py</p>
  <pre><code>def index(request):
      if request.method == 'POST':
          form = CityForm(request.POST)
          if form.is_valid():
              city = form.cleaned_data['city_name']
              API_KEY = 'MyAppKey'
              url = 'https://api.yelp.com/v3/businesses/search'
              headers = {'Authorization': 'Bearer {}'.format(API_KEY)}
              params = {'term':'bar','location':city}
              req = requests.get(url, params=params, headers=headers)
              parsed = json.loads(req.text)
              businesses = parsed[&quot;businesses&quot;]
              final_result = []
  
              for business in businesses:
                  results = {
                  'id': business['id'],
                  'business': business['name'],
                  'rating': business['rating'],
                  'image_url': business['image_url']
                  }
                  final_result.append(results)
  
              context = {'final_result': final_result}
              return render(request, 'API/results.html', context)
      else:
          form = CityForm()
      return render(request, 'API/home.html', {'form':form})
  
  def add_list(request):
      if request.method == 'POST':
          api_id = request.POST.get(&quot;id&quot;)
          Bar = BarList(api_id=api_id)
          Bar.save()
      else:
          return render(request, 'API/results.html')
  </code></pre>
  <p>Models.py</p>
  <pre><code>class BarList(models.Model):
      api_id = models.CharField(max_length=100)
  </code></pre>
  <p>Urls.py</p>
  <pre><code>urlpatterns = [
      path('', views.index, name='API-home'),
      path('list', views.list, name='API-list'),
      path('add/&lt;str:results&gt;/', views.add_list, name='API-add') ]
  </code></pre>
  <p>results.html template</p>
  <pre><code>{% extends &quot;API/base.html&quot; %}
  {% block content %}
  
  {% for results in final_result %}
  
  &lt;h3&gt;{{ results.business }}&lt;/h3&gt;
  
  &lt;form action=&quot;{% url 'API-add' results.id %}&quot; method=&quot;POST&quot;&gt;
    {% csrf_token %}
    &lt;input type=&quot;submit&quot; value=&quot;Add Item to List&quot; /&gt;
  &lt;/form&gt;
  
  {% endfor %}
  {% endblock content %}
  </code></pre>