I am building a very simple app that requests users to add the name of a city into a poll and then creates a frequency distribution of entered cities. My view for some reason is not retrieving City objects from the database by their name (primary key). Specifically, the first assignment after try:
is not going through, according to my trace statements. Must be overlooking something really obvious as I'm a novice. Please advise.
models.py:
from django.db import models
class City(models.Model):
name = models.CharField(max_length='20', primary_key=True)
count = 0
views.py:
def save(request):
name = request.POST['city_name']
try:
city = City.objects.get(pk=name)
print 'city does exist'
city.count += 1
except City.DoesNotExist:
print 'city does not exist'
city = City()
city.name = name
city.count += 1
city.save()
return render_to_response('thanks.html', {'city_name':name})