我有3个依赖下拉列表的乡村城市之路。
从数据库中预先填充国家/地区,然后根据第一个国家/地区的选择显示相关城市。
我能够从国家/地区列表中进行选择,并且它显示相关的城市,但是当我尝试选择城市并显示以下错误时:
未找到:/ getdetails2
如何解决此错误,其中 / getdetails2 是views.py的功能
class Country(models.Model):
name = models.CharField(max_length=100)
def __str__(self):
return str(self.name)
class City(models.Model):
name = models.CharField(max_length=100)
country = models.ForeignKey(Country,on_delete=models.CASCADE , related_name = 'cities')
def __str__(self):
# return'id : {0} MouhafazatID :{1} Name :{2}'.format(self.id,self.MouhafazatID,self.name)
return str(self.name)
class Road(models.Model):
Vil = models.CharField(max_length=100)
city= models.ForeignKey(City,on_delete = models.SET_NULL, null=True)
city= models.ForeignKey(City,on_delete = models.CASCADE, related_name='roads')
country= models.ForeignKey(Country,on_delete = models.SET_NULL,null=True)
def __str__(self):
return str(self.Vil)
$('select#selectcities').change(function () {
var optionSelected = $(this).find("option:selected");
var valueSelected = optionSelected.val();
var city_name = optionSelected.text();
data = {'ct' : city_name };
$.ajax({
type:"GET",
url:'/getdetails2',
// data:JSON.stringify(data),
data:data,
success:function(result){
console.log(result);
$("#selectroads option").remove();
for (var i = result.length - 1; i >= 0; i--) {
$("#selectroads").append('<option>'+ result[i].Vil +'</option>');
};
},
});
});
});
</script>
def getdetails2(request):
if request.method == 'GET' and request.is_ajax():
city_name = request.GET.get('ct', None)
print ("ajax city_name ", city_name)
result_set = []
all_roads = []
answer = str(city_name[1:-1])
print('answer = ' ,answer)
selected_city = City.objects.get(name=answer)
print ("selected city name ", selected_city)
all_roads = selected_city.roads.all()
print("roads are: " , all_cities)
for road in all_roads:
print ("road name", road.Vil)
result_set.append({'name': road.Vil})
return HttpResponse(simplejson.dumps(result_set),content_type='application/json')
# return JsonResponse(result_set,status = 200)
else:
return redirect('/')