我在Django和JQuery/Ajax中偶然发现了一个非常特殊的问题URL中有地址:
url(r'^app/insert$', Insert.as_view(), name="insert"),
url(r'^app/insert_ajax$', Insert_Ajax.as_view(), name="insert_ajax"),
url(r'^app/edit/(?P<id>d+)/$', Edit.as_view(), name="edit"),
正如您所看到的,它们都是基于对象的视图。还有一个模型:
class TheModel(models.Model):
item = models.ForeignKey(AnotherModel, related_name="anotherModel")
attribute = models.ForeignKey(ListOfAttributes, related_name="attributes", blank=True)
以及基于给定模型的形式:
class TheModelForm(forms.ModelForm):
class Meta:
model = TheModel
因此,问题是属性必须根据给定的项目进行更改(筛选)。有一个JQuery可以处理这个问题:
var change_attribute = function(){
var selected_item_id = $("#selected_item_id").val();
$.post("insert_ajax",{"method":"get_attributes","item":$("#id_item").val()}, function( data ) {
$("#id_attributes").empty();
$.each(data,function(index, value){
if(value['id'] == selected_item_id){
$("#id_attributes").append("<option selected='selected' value='"+ value['id'] +"'>"+value['name']+"</option>");
}else{
$("#id_attributes").append("<option value='"+ value['id'] +"'>"+value['name']+"</option>");
}
});
});
}
这直接进入Ajax视图:
class CallDropAjax(View):
def post(self, request):
method = request.POST.get('method', None)
context = {}
if method:
try:
context = getattr(self, method)(request)
except AttributeError as e:
context = json.dumps({'success': False,
'error': 'Method %s cannot be called due to %s.' % (method,
str(e))})
else:
context = json.dumps({'success': False,
'error': 'No method specified'})
return HttpResponse(context, content_type="json/application")
def get_attributes(self, request):
attributes = ListOfAttributes.objects.filter(
item__id=request.POST.get('item'))
json_op = []
for attribute in attributes:
json_op.append({"id": attribute.id,
"name": attribute.name})
return json.dumps(json_op)
在插入和编辑视图/表单中都使用相同的JQuery脚本,但它仅适用于插入,而不适用于编辑当我查看数据时,插入正确地要求服务器提供
http://the_server/app/insert_ajax
因此服务器做出响应,并相应地过滤和修改属性的下拉列表。但在版本视图中,它不起作用,当我研究ajax向服务器请求什么时,结果是这样的:
http://the_server/app/edit/2453/insert_ajax
当然,这是错误的,所以脚本不会接收任何数据,也不会修改任何内容(它只是将所有数据留在下拉列表中)。
所以我的问题是:为什么会发生这种情况,我该如何解决?如何使此脚本在编辑视图和插入视图中都有效?
I假设/app/edit/2453/
是一个可查看的url。
当您编辑/app/edit/2453/
的内容时,JQuery会向url
+insert_ajax
发出AJAX POST请求。
参见此行:
$.post("insert_ajax",{"method":"get_attributes","item......
您可以通过将编辑页面的"insert_ajax"替换为完整的相对url(/app/edit/2453/
)来修复这种行为。
我解决了它!
我不得不更改urls.py并添加另一行:
url(r'^app/insert$', Insert.as_view(), name="insert"),
url(r'^app/insert_ajax$', Insert_Ajax.as_view(), name="insert_ajax"),
url(r'^app/edit/(?P<id>d+)$', Edit.as_view(), name="edit"),
url(r'^app/edit/insert_ajax$', Insert_Ajax.as_view(), name="insert_insert_ajax"),
因此,现在可以在edit中调用该脚本,它会发现它回到了相同的视图处理程序Insert_Ajax。
此外,我还必须修改JQuery脚本,以便它同时运行对insert_ajax和insert_insert_ax:的调用
var post_change = function(){
var selected_id = $("#selected_id").val();
$.post("insert_ajax",{"method":"get_attributes","item":$("#id_item").val()}, change_item);
$.post("insert_edit_ajax",{"method":"get_attributes","item":$("#id_item").val()}, change_item);
}
并将响应处理程序抛出到另一个函数"change_item"(这样我就不必复制+粘贴代码了。
它起作用了!这不是一个非常优雅的解决方案,同时对两个视图进行冗余调用,希望其中一个视图响应,但目前可以。也许我稍后会在学习如何检查URL调用是否成功时更改它。