Wagh
Wagh

Reputation: 4306

Store the jquery variable in django backend

I want to store the variable which is declared in jquery to django backend.

.js

<script>
var total=5;
</script>

i want to store the value of total i.e 5 to score field in total1 model class.

i have one model in django project

model.py

class total1(models.Model):    
    score = models.IntegerField(max_length = 50, blank = True,null = True)

i want to store the value of total to score field in backend.

what should i need to write in view and in java script.

Please help me.

Upvotes: 1

Views: 538

Answers (2)

Wagh
Wagh

Reputation: 4306

I think this will help you,,,,

your html:

$('#myButton').click(function(){
  var bjpFan=localStorage['bjpFan'];
  var userid = //get the user id here to which you want to relate the score.
  var total = parseInt(localStorage['total']);
  $.ajax({
    url: "/poll/score/",
    type:"GET",
    data: {total:total,userid:userid}
  }).done(function(data){
     alert(data);//do what you want to do with response
  });
}); 

your urls.py:

url(r'/poll/score/$', 'score', name='score'),

your views.py

def score(request):
   if request.is_ajax():
     if request.method == 'GET':
        user = UserProfile.objects.get(id=request.GET.get('userid'))
        user.score = request.GET.get('total')
        user.save()
        return HttpResponse("%s" % user.score )

Hope this will help you

Upvotes: 1

Dr.Elch
Dr.Elch

Reputation: 2225

You'll need a <form> to pass values back to your view and in this form you will need a field that is associated with your variable total. Django offers some powerful helpers to achieve your goal.

You really need to look at the django documentation: https://docs.djangoproject.com/en/dev/topics/forms/

How it basically works:

you have A model like yours:

models.py:

class total1(models.Model):    class total1(models.Model):    
    score = models.IntegerField(max_length = 50, blank = True,null = True)

you will need a form.py which holds a form that is generated by your model like so:

forms.py:

 class YourTotalForm(ModelForm):
     class Meta:
         model = total1
         widgets = { 
        'score': forms.HiddenInput(), #makes the textfield hidden, so you can deal with it via jQuery
    }   

now you can get an instance of your form like so:

views.py:

def yourview(request):        
# get a new instance of a total1 Form 
form = YourTotalForm()

return render_to_response("mytemplate.html", {
     "form": form,

now, go to your template file and output the form object. you will get a visible text field which you can use to to manipulate the score field. Of course that is not what you are looking for, because you use jquery to manipulate this value. You need to modify the textfield widget to use a hidden field. Which is what we did in the form class

mytemplate.html:

<form action="" method="POST">
    {{form}}
</form>

Now you should see nothing but a submit button. However if you inspect the html output you will find a hidden form field which you can select with jquery to give it the correct value. When you hit submit, you will need your view to store the value in the database. But just look at the provided documentation at the beginning of my answer to find out how to do that.

Upvotes: 2

Related Questions