我在模板中有一个表单,用户可以在其中输入地址。我想使用Google API应用“地方自动填充地址”。
但是,由于某种原因,自动填充功能不适用于表单。任何建议都会被贬低。谢谢。
Forms.py
options(stringsAsFactors=FALSE)
library(XML)
#get website
tfile <- tempfile()
download.file("http://cepea.esalq.usp.br/frango/?page=379&Dias=15", tfile)
temp <- readHTMLTable(tfile)
#read in table
tbl <- temp[[5]][!is.na(temp[[5]]$V2) & !is.na(temp[[5]]$V4),]
tbl2 <- split(tbl, cumsum(tbl$V1==""))[[2]][-1,]
tbl2
#perform formatting
colnames(tbl2) <- c("Date","Price","Pct1","Pct2")
tbl2 <- data.frame(apply(tbl2, c(1,2), function(x) {
x <- gsub("Â","",x)
x <- gsub(",",".",x,fixed=TRUE)
x <- gsub("%","",x)
}))
tbl2$Date <- as.Date(tbl2$Date," %d/%m/%Y ")
tbl2$Price <- as.numeric(tbl2$Price)
tbl2$Pct1 <- as.numeric(tbl2$Pct1) / 100
tbl2$Pct2 <- as.numeric(tbl2$Pct2) / 100
tbl2
# Date Price Pct1 Pct2
#65 2016-08-17 4.37 0.0046 0.0817
#66 2016-08-16 4.35 0.0046 0.0767
#67 2016-08-15 4.33 0.0046 0.0718
#68 2016-08-12 4.31 0.0000 0.0668
#69 2016-08-11 4.31 0.0070 0.0668
#70 2016-08-10 4.28 0.0047 0.0594
#71 2016-08-09 4.26 -0.0070 0.0545
#72 2016-08-08 4.29 0.0387 0.0619
#73 2016-08-05 4.13 0.0049 0.0223
#74 2016-08-04 4.11 0.0000 0.0173
#75 2016-08-03 4.11 0.0173 0.0173
#76 2016-08-02 4.04 0.0000 0.0000
#77 2016-08-01 4.04 0.0000 0.0000
#78 2016-07-29 4.04 0.0000 -0.0049
#79 2016-07-28 4.04 -0.0025 -0.0049
Html模板
where_load = forms.CharField(max_length=100, widget=forms.TextInput(attrs={'id':"autocomplete"}))
where_unload = forms.CharField(max_length=100, widget=forms.TextInput(attrs={'id':"autocomplete"}))
答案 0 :(得分:1)
在回调中,找到要应用自动填充功能的元素,然后在那里声明自动填充功能。您也可以在模板中加载api密钥,就像我在下面所做的那样
<script>
var onLoaded = function() {
// I am assuming your field has id of where_load, it might be different
var location_input = document.getElementById('where_load');
var autocomplete = new google.maps.places.Autocomplete(location_input);
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?key={{api_key}}&libraries=places&callback=onLoaded" async defer></script>