e1sordo
e1sordo

Reputation: 29

How to send form data from Angularjs to Django

I did the application on the sample from this lesson. Here, using DRF, a list of all added games is displayed on the page. I would really like to learn how to write a simple form of adding a new record to the database (two fields: title and description [as in the example]).

With js, I'm not very familiar with so far, so I do not know which side to get close to solving the problem.

Upvotes: 0

Views: 552

Answers (2)

mukesh mali
mukesh mali

Reputation: 104

  $scope.saveUser = function(event) {

        postForm({ id: 0 }, $('#FormName'), $scope, function(data) {

        })
    }

 function postForm(postInfo, form, $scope, callback,) {    

    var postData = new FormData(form.get(0));

    $.each(postInfo, function(key, value) {
      postData.append(key, value);
    });

    $.ajax({
      type: form.attr('method'),
      url: form.attr('action'),
      data: postData,
      cache: false,
      dataType: 'json',
      processData: false,
      contentType: false,
      headers: {
        "X-CSRFToken": app.getStorage("csrftoken")
      },
      beforeSend: function() {
        $('#loading-image').show();        
      },
      complete: function() {
        $('#loading-image').hide();
        if(typeof saveButtonId !== typeof undefined) {
          $('#'+saveButtonId).removeAttr('disabled');
        }
      },
      success: function(data) {


      },
      error: function(data) {

        // 
      }
    });
  };

Upvotes: 1

Boris Sitsker
Boris Sitsker

Reputation: 19

you'd be updating code in your mysite/backend folder to have some incoming route to insert data into django db using some serializer

sorry I don't have more specific details, but just wanted to convey the general idea

Here's some more information on Django serializers: http://www.django-rest-framework.org/api-guide/serializers/

another tutorial on adding an additional route to django could help

Upvotes: 1

Related Questions