How to add data from javascript in databases django

Viewed 32

I'm new with Django Framework. I made a template in html using a script javascript. It looks like a chat. I want to put answers from user in my databases.

Here is the link with my code: https://codepen.io/maria-lupu-the-sasster/pen/abGdgPQ

And the end, after the "conversation" is over and appear the button "Let's go", I want all the details (that 'uid' and name) to be added in my databases.

There are my model where I want to add the details:

class Employee(models.Model):
    employee_id = models.BigAutoField(primary_key=True)
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    u_id=models.CharField(max_length=20)
    badge_number=models.CharField(max_length=50)
    email_adress = models.CharField(max_length=300)
    role = models.CharField(max_length=40)

    def __str__(self):
        return self.last_name
  • for first_name and last_name I was thinking of doing a split to the string provided by the user (that time when the bot from chat ask user for his name)
  • email_adress will be composed with uid+@gmail.com

I searched on google for many solutions, but I still don't know how to do it. I saw something with Ajax, but I don't know how to work with this. Please, can someone help me and explain me?

1 Answers

So this is an example Ajax Post for django and a basic view (might be better to use forms)

If being logged in is required you need to place: {% csrf_token %} somewhere in the template and csrfmiddlewaretoken needs to be included with the post.. or maybe you always need it?- idk i've always included it.

function save(employee_id, first_name, last_name, u_id, badge_number, email_adress, role){
    postData = {
        'employee_id': employee_id,
        'first_name': first_name,
        'last_name': last_name,
        'u_id': u_id,
        'badge_number': badge_number,
        'email_adress': email_adress,
        'role': role,

        // if login is required you need to pass this, 
        'csrfmiddlewaretoken': $('[name=csrfmiddlewaretoken]').val(),
    };

    $.ajax({
        method: 'post',
        url: 'url_to_save_view', // put your URL here
        data: postData,
        success: function(data){
            console.log(data);
            // do things
        },
        error: function(event,xhr,settings,errorText){
          alert('error');
        }
    });
}
def saveconvo(request):
    if request.method == 'POST':

        Employee.objects.create(
            employee_id=request.POST.get('employee_id'),
            first_name=request.POST.get('first_name'),
            last_name=request.POST.get('last_name'),
            u_id=request.POST.get('u_id'),
            badge_number=request.POST.get('badge_number'),
            email_adress=request.POST.get('email_adress'),
            role=request.POST.get('role'),
            )

        return HttpResponse(
            json.dumps({'status':True),
            content_type="application/json"
        )
Related