问题描述:
1]我有一个具有国家和城市属性的模型
class UserReportedData(db.Model):
#country selected by the user, this will also populate the drop down list on the html page
country = db.StringProperty(default='Afghanistan',choices=['Afghanistan','Aring land
Islands','Albania'])
#city selected by the user
city = db.StringProperty()
相关表格
class UserReportedDataForm(djangoforms.ModelForm):
class Meta:
model = UserReportedData
2]在stackoverflow的帮助下(感谢Chris,发布:GeoIP with google app engine and python),我已经找到了如何在html页面中使用javascript来查找用户的国家和城市。以下代码可以成功地在页面上打印国家和城市的值
<!-- including the geoip javascript library -->
<script
src="http://j.maxmind.com/app/geoip.js"
type="text/javascript">
</script>
<script type="text/javascript">
if (window.geoip_country_name) {
document.write(geoip_country_name())
document.write(geoip_city())
}
</script>
3]现在显示页面时,国家和城市文本框和默认值来自django表单(抱歉因为限制而无法发布图片)
问题:
我希望在国家/地区和城市文本框中填写用户的国家/地区和城市数据。因此,python表单或模型应该能够调用javascript代码并获取用户的国家和城市数据。
谢谢,
[编辑#1]根据@Haochi建议的方法进行编辑
以下是djangoforms
生成的id<select name="country" id="id_country">
<input type="text" name="city" id="id_city" />
在html页面中尝试了以下javascript
<script type="text/javascript">
if (window.geoip_country_name) {
document.getElementById("id_country").value = geoip_country_name();
document.getElementById("id_city").value = geoip_city();
document.write("this is a javasctipt print"); /* this prints properly*/
document.write(document.getElementById("id_country").value);
document.write(document.getElementById("id_city").value);
}
</script>
然而,这并没有恰当地填写id_country和id_city文本字段的值。
[编辑#3]这就是HTML文件的样子,我现在已将GEOIP计算从服务器端移动到客户端
<!-- Text box for city, later on in the design this will be a drop down box -->
<p> Select City: </p>
<div>
<input type="text" name="city" id="city">
<!-- By default, we will select users city -->
<script type="text/javascript" language="JavaScript">
document.getElementById("city").value = geoip_city()
</script>
</div>
<p> Select Country: </p>
<select name="selCountry" id="country">
<!-- By default, we will select users country -->
<script type="text/javascript" language="JavaScript">
document.write("<option value=\"" + geoip_country_name() + "\" selected>"
</script>
<option value="United States">
United States
</option>
<option value="United Kingdom">
United Kingdom
</option>
我认为最佳做法是将所有javascript移动到单独的文件中,我需要尝试此操作并相应地修改HTML代码。
答案 0 :(得分:0)
假设这两个输入的ID是国家和城市:
document.getElementById("country").value = geoip_country_name()
document.getElementById("city").value = geoip_city()
答案 1 :(得分:0)
我编写了以下python类来查找用户的国家和城市。 这是有效的,如果有人发现任何可以改变效率的问题或事情,请告诉我
from google.appengine.api import urlfetch
#class that is used for geo ip functionality of the application
class GeoIpLocator:
#method that will return the country city dictionary based on users GeoIP location
#dictionary format {"country_name": "USA", "city":"chicago"}
def get_country_city_dictionary(self):
url = "http://j.maxmind.com/app/geoip.js"
result = urlfetch.fetch(url)
if result.status_code == 200:
keep = ['country_name', 'city']
results = dict([x for x in re.findall("geoip_(.*?)\(.*?\'(.*?)\'", result.content) if x[0] in keep])
results.update({'source': 'mmind'})
if results:
return results
else:
return {}
[编辑#1]这不起作用,并将返回python应用程序加载的服务器的IP,感谢@Calvin指出这一点。