我有一个表单,在用户选中“地址”字段后触发对视图的ajax请求。它检索邮政编码,然后使用相同的zip填充传感器。问题是,如果有任何形式错误,我从get_pickups视图生成的结果的下拉将丢失。如何保留它,即使在表单错误上也会保留结果
def get_pickups(request):
if request.is_ajax():
# Get available pickup dates based upon zip code
zip = request.POST.get('zip',None)
routes = Route.objects.filter(zip=zip).values_list('route',flat=True)
two_days_from_today = date.today() + relativedelta(days = +2)
submitted_from = request.POST.get('template',None)
if submitted_from == '/donate/':
template = 'donate_form.html'
results = PickupSchedule.objects.filter(route__in=routes,date__gt = two_days_from_today, current_count__lt=F('specials')).order_by('route','date')
else:
template = 'donate-external.html'
results = PickupSchedule.objects.filter(route__in=routes,date__gt = two_days_from_today, current_count__lt=F('specials')).order_by('date')
return render_to_response(template,{ 'zip':zip, 'results':results}, context_instance=RequestContext(request))
通过jquery调用我的ajax:
$.ajax({
type: "POST",
url:"/get_pickups/",
data: {
'zip': zip,
'template':template
},
success: function(data){
results = $(data).find('#results').html()
$("#id_pickup_date").replaceWith("<span>" + results + "</span >");
},
error: function(){
alert("Error");
}
答案 0 :(得分:1)
我可以想到两种方法:
我会使用你最熟练的解决方案(python / django或javascript)