从pyusps解析地址信息

时间:2012-11-16 15:54:04

标签: python django api

我正在使用Pyusps(Python USPS Api)来验证地址是否确实存在。我创建了一个表单来捕获数据;我很难从api接收数据并将其显示在下一页上。任何帮助将不胜感激。

#views.py

def address(args):
    address = AddressForm['street']

def city(args):
    city = AddressForm['city']

def state(args):
    state = AddressForm['state']

def zip5(args):
    zip5 = AddressForm['zip_code']


def address_view(request):
if request.method == 'POST':
    form = AddressForm(request.POST)
    if form.is_valid():
        try:
            from pyusps import address_information
            addr = dict([
                ('address', {'address': address}),
                ('city', {'city': city}),
                ('state', {'state': state}),
                ('zip5', {'zip_code': zip5}),
            ])
            address_information.verify('API_ID', addr)
            a = dict([
                ('address', '6406 IVY LN'),
                ('city', 'GREENBELT'),
                ('state', 'MD'),
                ('zip5', '20770'),
                ('zip4', '1441'),
                ])
            c = {'form': a}
            return render_to_response('address_change.html', c, context_instance=RequestContext(request))
        except ValueError:
            pass

else:
    form = AddressForm()
c = {'form': form}
return render_to_response('address_change.html', c, context_instance=RequestContext(request))

#models.py
class Address(models.Model):
user = models.ForeignKey(User)
street = models.CharField(max_length=50)
city = models.CharField(max_length=50)
state = models.CharField(max_length=50)
zip_code = models.CharField(max_length=50)


# Pyusps API Documentation
from pyusps import address_information

 addr = dict([
 ('address', '6406 Ivy Lane'),
 ('city', 'Greenbelt'),
 ('state', 'MD'),
 ])
 address_information.verify('foo_id', addr)
  dict([
('address', '6406 IVY LN'),
('city', 'GREENBELT'),
('state', 'MD'),
('zip5', '20770'),
('zip4', '1441'),
])

1 个答案:

答案 0 :(得分:0)

我想我看到了你的问题,这个:

addr = dict([
                ('address', {'address': address}),
                ('city', {'city': city}),
                ('state', {'state': state}),
                ('zip5', {'zip_code': zip5}),
            ])

应该是:

addr = dict([
                ('address', address),
                ('city', city),
                ('state', state),
                ('zip5', zip5),
            ])