我通过MultipleChoiceFiel创建一个多选表单。我想从数据库中选择选项并加载该选项的信息。我通过ajax传递fruit_id来查看获取对象的结果,如何从视图发送到我的HTML接收json_string数据:
$(document).ready(function(){
$("#mySelect").change(function(){
fruit_id = $("#mySelect option:selected").val();
$.ajax({
type: 'POST',
dataType: 'json',
url: '{% url 'view' %}',
data: {
'fruit_id': fruit_id
},
success: function(result) {
document.write(result)
}
});
});
});
AJAX:
def view(request):
if request.method == 'POST':
if request.is_ajax():
id = request.POST.get('fruit_id')
fruit = Fruit.object.get(pk=id);
data = {}
data['fruit_name'] = fruit.name
data['fruit_code'] = fruit.code
data['fruit_address'] = fruit.address
json_string = json.dumps(data)
return JsonResponse({'json_string': json_string})
view.py
public static void main(String[] args) {
Queue<String>Q=new ArrayQueue<String>();
Stack<String>S=new ArrayStack<String>();
Q.enqueue("Bob");
Q.enqueue("Tom");
Q.enqueue("Ann");
Q.enqueue("Bill");
Q.enqueue("David");
Q.enqueue("Mary");
Q.enqueue("Bob");
Q.enqueue("Jane");*/
for(int k=1;k<=9;k++){
insert(Q,S,k);
System.out.print("Dopo aver invocato insert con k="+k +", S = < ");
while(!S.isEmpty())
System.out.print(S.pop()+" ");
System.out.println(">");
}
}
public static <E>void insert(Queue<E> Q,Stack<E> S, int k){
int sizeOfQ=Q.size();
if(sizeOfQ<k)
return;
for(int i=0;i<k;i++){
E elt=Q.dequeue();
Q.enqueue(elt);
}
for(int i=0;i<sizeOfQ;i++){
E elt=Q.dequeue();
if(i>=sizeOfQ-k){
S.push(elt);
Q.enqueue(elt);
}else{
Q.enqueue(elt);
}
}
for(int i=0;i<sizeOfQ-k;i++){
Q.enqueue(Q.dequeue());
}