2

So I have an AJAX command, which passes information to a views.py method (I have verified that the passing works from the HTML->urls.py->views.py, so that's all good), but once I have it in "views.py", I have no idea how to get it to update in the database itself.

I have tried to avoid using a forms.py file if possible, but if that is the only option I'll bend to it.

The AJAX function is as follows:

$.ajax({
    url : '/perform/acts/update/{{  act.id  }}/',
    type : "POST",
    data : {
        'csrfmiddlewaretoken' : "{{  csrf_token  }}",
        furtherData : furtherData
    },
    success : function(result) {}
});

The views.py function is...so far, lacking, to say the least, but this is where I'm sort of lost:

def update_act(request, furtherData_id):
    if request.method == 'POST':
      ?
      return HttpResponse(?)

A big reason for doing this this way was performing updates without reloading and without having to add another module. I have been using Django for only a couple weeks, so it could be something easy that I'm missing...

Any help much appreciated!

2 Answers 2

4
def update_act(request,furtherData_id):
    from django.http import JsonResponse
    if request.method == 'POST': 
        obj=MyObj.objects.get(pk=furtherData_id)
        obj.data=request.POST['furtherData']
        obj.save()
        return JsonResponse({'result':'ok'})
    else:
        return JsonResponse({'result':'nok'}
Sign up to request clarification or add additional context in comments.

1 Comment

I feel like this is the "right answer" based on the parameters the OP provided, but this does blindly save the input to the DB. Using a ModelForm and supplying obj as an instance to that form would provide some data validation here.
2

Your view function:

def my_view_action(request, any_pk_id):
    from django.http import JsonResponse
    if request.method=='POST' and request.is_ajax():
        try:
            obj = MyModel.objects.get(pk=any_pk_id)
            obj.data_attr = request.POST['attr_name']
            obj.save()
            return JsonResponse({'status':'Success', 'msg': 'save successfully'})
         except MyModel.DoesNotExist:
            return JsonResponse({'status':'Fail', 'msg': 'Object does not exist'})
     else:
         return JsonResponse({'status':'Fail', 'msg':'Not a valid request'}


This function directly save data on your DB, For validate it first use form then proceed to save action.

---Steps---
- Create a form for model.
- Fill data on this model via request/object.
- Run validate on form then save to db via form or via model.

For more info https://docs.djangoproject.com/en/1.10/topics/forms/modelforms/#the-save-method

1 Comment

To access form data in the views just do request.POST

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.