I stumbled upon a very peculiar problem with Django and JQuery/Ajax
There are addresses in URLs:

```
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"),

```

As you can see, they're all object-based views. There is also a model:

```
class TheModel(models.Model):
    item = models.ForeignKey(AnotherModel, related_name="anotherModel")
    attribute = models.ForeignKey(ListOfAttributes, related_name="attributes", blank=True)

```

and a form based upon given model: 

```
class TheModelForm(forms.ModelForm):
    class Meta:
        model = TheModel

```

So the deal is that attribute has to change (filter) according to given item. There is a JQuery that handles that:

```
    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>");
            }
        });
    });

}

```

And this goes straight to Ajax view:

```
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)

```

**The same JQuery script is used in both insert and edit views/forms, but it works only for insertion, and not edition.** When I looked into the data, the insertion properly asks the server for

```
http://the_server/app/insert_ajax

```

so the server responds, and the drop-down list for Attributes is filtered and modified accordingly. But in the edition view it does not work, and when I looked into what ajax asks the server for, it came out like this:

```
http://the_server/app/edit/2453/insert_ajax

```

which is, of course, wrong, so the script won't receive any data and will not modify anything (it just leaves ALL data in drop-down list).

So my question is: why is this happening and how can I fix it? How can I make this script works in both - edition and insertion views?