python - In Django view,How to save array which pass from template via Ajax, to database? -
i create array in javascript , send via ajax view , can in view.now want save database(mysql) "internal error" ajax.i think ajax when post data view , django want connect mysql database, connection ajax abroted.
this template ajax:
var posturl = "http://localhost:8000/student/{{id}}/student_add_course/"; $('form[name="myform"]').submit(function(){ $.ajax({ url:posturl, type: "post", // stock somthing this.stock=[[1,12],[2,14],...] data: {'stock': stock, 'counter':i,csrfmiddlewaretoken: '{{ csrf_token }}'}, error:function (xhr, textstatus, thrownerror){ alert(thrownerror);}, }) });
this view:
def student_add_course(request,student_id): if request.method=='get': context={'id':student_id , 'form':addcourseforstudentform()} request.session['listofcourses']=[] return render(request, 'student/addcourseforstudentform.html',context) elif request.is_ajax(): listofcourses=course_passes.objects.order_by('-id') id =listofcourses[0].id counter=int(request.post.get('counter')) in range(0,counter): selected_course=request.post.getlist('stock[%d][]'%i) //course_passes table. // evrey thing until here when want save it,i error. #a.save() return render(request, 'student/add_course.html')
what code problem , how should solve it? there better way this?
i'm sorry bad english.
edit:
finally understand problem in mysql side. value want insert not correct , change
a=course_passes(id+1,int(selected_course[0]),int(selected_course[1]),int(student_id))
to
find_user=user.objects.get(user_code=student_id,task=2) a=course_passes(id+1,selected_course[0],selected_course[1],find_user.id)
thanks comment.
Comments
Post a Comment