嗨,我有一个小的 javascript 函数,可以动态过滤我的表单:
// set up a new XMLHttpRequest variable
var request = false;
try {
request = new XMLHttpRequest();
} catch (trymicrosoft) {
try {
request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (othermicrosoft) {
try {
request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (failed) {
request = false;
}
}
}
// if no request then throw up an alert window
if (!request)
alert("Error initializing XMLHttpRequest!");
// this function takes the island group value and sends it to the url as a get variable
function getIslandName(lang) {
var islandGroup = document.getElementById("id_island_group").value;
if (islandGroup == '') {
// if Not Specified re-selected then set data to null and bypass updatePage()
var data = null;
update_select($('select[name=island_name]'), data);
}
else {
var url = "../collections/find_island?island_group=" + escape(islandGroup);
// alert("Ready state is: " + request.readyState);
// alert("url: " + url);
request.open("GET", url, true);
request.onreadystatechange = updatePage;
request.send(null);
}
}
// what to do when http ready state changes
function updatePage() {
if (request.readyState == 4) {
if (request.status == 200) {
// get response array
var data = JSON.parse(request.responseText);
update_select($('select[name=island_name]'), data);
}
// some error checking
else if (request.status == 404) {
alert("Request url does not exist");
}
else {
alert("Error: status code is " + request.status);
}
}
}
function update_select(select, data) {
// find and remove existing options
select.find('option').remove();
var optionText = document.getElementById("lang").innerHTML
select.append($('<option value>' + optionText + '</option>'));
// loop through results and append to select as options
for (var k in data) {
select.append($('<option value="'+data[k]+'">'+data[k]+'</option>'));
}
}
我的表格:
class SearchForm(forms.Form):
...
island_group = forms.ModelChoiceField(
required=False,
label=ugettext_lazy('Island Group'),
initial=None,
queryset=Localityonline.objects.values_list('islandgroup', flat=True).distinct('islandgroup').exclude(islandgroup=None).order_by('islandgroup'),
empty_label=ugettext_lazy("Not Specified"),
widget=forms.Select(attrs={"class":'searchfield', "onChange":'getIslandName()'})
)
island_name = forms.ChoiceField(
required=False,
label=ugettext_lazy('Island Name'),
initial=None,
choices=DEFAULT_CHOICES,
#queryset = Localitymayor.objects.values_list('islandname', flat=True).distinct('islandname').exclude(islandname="n/a").order_by('islandname'),
#empty_label=ugettext_lazy("Not Specified"),
)
...
问题是,如果值islandGroup
类似于“Española”(即拉丁字符),我会收到状态 500 错误和以下 Django 错误:
OperationalError: (1267, "Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation '='")
<WSGIRequest
path:/datazone/collections/find_island/,
GET:<QueryDict: {u'island_group': [u'Espa\ufffd']}>,
POST:<QueryDict: {}>,
COOKIES:{'__utma': '173650102.812455619.1337018914.1337629222.1337641044.38',
'__utmb': '173650102.4.10.1337641044',
'__utmc': '173650102',
'__utmz': '173650102.1337613083.36.2.utmcsr=darwinfoundation.org|utmccn=(referral)|utmcmd=referral|utmcct=/datazone/galapagos-research/search/',
'csrftoken': '7cac07481c8ff5762fac33bf0b3590da',
'sessionid': '4becce44a950091a3cf7d306633427b4'},
META:{'CSRF_COOKIE': '7cac07481c8ff5762fac33bf0b3590da',
'DOCUMENT_ROOT': '/home/darwinfo/public_html',
'GATEWAY_INTERFACE': 'CGI/1.1',
'HTTP_ACCEPT': '*/*',
'HTTP_ACCEPT_CHARSET': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
'HTTP_ACCEPT_ENCODING': 'gzip,deflate,sdch',
'HTTP_ACCEPT_LANGUAGE': 'en-US,en;q=0.8',
'HTTP_CONNECTION': 'close',
'HTTP_COOKIE': 'csrftoken=c297f86b353937f52abc36af8b4d595a; csrftoken=7cac07481c8ff5762fac33bf0b3590da; sessionid=4becce44a950091a3cf7d306633427b4; __utma=173650102.812455619.1337018914.1337629222.1337641044.38; __utmb=173650102.4.10.1337641044; __utmc=173650102; __utmz=173650102.1337613083.36.2.utmcsr=darwinfoundation.org|utmccn=
...
'HTTP_USER_AGENT': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.168 Safari/535.19',
'PATH': '/sbin:/usr/sbin:/bin:/usr/bin',
'PATH_INFO': u'/collections/find_island/',
'PATH_TRANSLATED': '/home/darwinfo/public_html/collections/find_island/',
'QUERY_STRING': 'island_group=Espa%F1ola',
'REDIRECT_QUERY_STRING': 'island_group=Espa%F1ola',
'REDIRECT_STATUS': '200',
'REDIRECT_UNIQUE_ID': 'T7rLMjIWL2YAAGS3N@sAAAET',
'REDIRECT_URL': '/datazone/collections/find_island/',
'REMOTE_ADDR': '157.100.37.112',
'REMOTE_PORT': '35245',
'REQUEST_METHOD': 'GET',
'REQUEST_URI': '/datazone/collections/find_island/?island_group=Espa%F1ola',
'SCRIPT_FILENAME': '/home/darwinfo/public_html/datazone/django.fcgi',
'SCRIPT_NAME': u'/datazone',
'SERVER_ADDR': '50.22.47.102',
'SERVER_PORT': '80',
'SERVER_PROTOCOL': 'HTTP/1.1',,
'SERVER_SOFTWARE': 'Apache',
'UNIQUE_ID': 'T7rLMjIWL2YAAGS3N@sAAAET',
'wsgi.errors': <flup.server.fcgi_base.OutputStream object at 0x1707ab90>,
'wsgi.input': <flup.server.fcgi_base.InputStream object at 0x16f2fad0>,
'wsgi.multiprocess': False,
'wsgi.multithread': True,
'wsgi.run_once': False,
'wsgi.url_scheme': 'http',
'wsgi.version': (1, 0)}>
任何帮助将非常感激。