我正在尝试在我的 urls.py 中为 json 查询编写一个 url 正则表达式,该查询将采用如下所示的 url:
http://localhost:8000/structures/hydrants/3/json?address=100+Washington+Road,+Sacramento,+CA
并将其指向这样的视图:
def hydrant_json(request, hydrant_address):
hydrant = Hydrant.objects.filter(address = hydrant_address)
data = [hydrant.json()]
return HttpResponse(json.dumps(data), content_type='application/json')
地址定义如下:
class Address(models.Model):
class Meta:
verbose_name_plural = "addresses"
street_num = models.IntegerField(default=0)
street = models.CharField(max_length=200)
city = models.CharField(max_length=200)
state = models.CharField(max_length=200)
def json(self):
fields = ('street_num', 'street', 'city', 'state')
return {
'street_num': str(self.street_num),
'street': str(self.street),
'city': str(self.city),
'state': str(self.state)
}
def __unicode__(self):
return str(self.street_num) + ' ' + self.street + ', ' + self.city + ', ' + self.state
和 hydrant.json() 工作,一切都是花花公子在视图方面。我只希望正则表达式以上面显示的格式接收一个确切的地址,适合查询 URL。
谢谢!