Django I';我正在尝试使用ajax获取特定数据,我可以';我做得不好
我试图使用ajax从数据库中获取特定数据,我可以使用'all()'方法获取所有数据。 但我不能只获得特定的数据 ajax代码Django I';我正在尝试使用ajax获取特定数据,我可以';我做得不好,django,ajax,Django,Ajax,我试图使用ajax从数据库中获取特定数据,我可以使用'all()'方法获取所有数据。 但我不能只获得特定的数据 ajax代码 <script type="text/javascript"> $(document).ready(function(){ $("#request-btn").click(function(){ $.ajax({ url: "{% url
<script type="text/javascript">
$(document).ready(function(){
$("#request-btn").click(function(){
$.ajax({
url: "{% url 'contact_details' %}",
type: "GET",
data: { property: "{{ property.id }}" },
dataType:"json",
success: function(result){
$.each(result, function() {
$("#request-btn").hide()
html = "<h6>Contact me" + "<br>" + "Email:" + this.email + "<br>" +"Mobile:" + this.mobile + "<hr>"
$("#response").append(html)
});
}
});
});
});
它抛出了错误
TypeError: 'Property' object is not iterable
在“all()”方法中,它提供所有数据,
对于测试,我在代码中做了一些更改(将“many=True”更改为“many=False”)
但它给出了未定义的值和重复显示的输出
这里我使用的是序列化程序。
序列化程序.py
def contact_details(request):
property_id = request.GET.get('property')
if request.user.is_authenticated:
property_details = Property.objects.get(id=property_id)
seraializer = PropertySerializer(property_details, many=True)
details = seraializer.data
return JsonResponse(details, safe=False )
else:
return JsonResponse({"authenticated": False})
class PropertySerializer(serializers.ModelSerializer):
class Meta:
model = Property
fields = ('id', 'email', 'mobile')
我需要电子邮件和移动数据,如何解决这个问题
class PropertySerializer(serializers.ModelSerializer):
class Meta:
model = Property
fields = ('id', 'email', 'mobile')