[英]How can i send my template values to database using Jquery, Ajax in django?
1.jquery 1.jquery
$(textbox).on('click','.save',function(e){
e.preventDefault();
var x = $('#input_msg').val();
$.ajax({
url:'newpostx/',
type: $(this).attr('method'),
data: x,
headers:{
'X-CSRFToken':'{{csrf_token}}'
}
}).done(function(msg) {
document.location = "http://127.0.0.1:8000/newpostx/"
alert("save data")
}).fail(function(err){
alert('no data was saved')
})
});
$(container).on('click','.show', function () { //On click of link, textbox will open console.log(count[1]) msg_bubble = 1; $("div.popup").show(); var num = $(this).attr('value'); console.log(num,'num') $(textbox).append('Grid No_ '+ num +'$(container).on('click','.show', function () { //点击链接,文本框会打开 console.log(count[1]) msg_bubble = 1; $("div.popup" ).show(); var num = $(this).attr('value'); console.log(num,'num') $(textbox).append('Grid No_'+ num +'
Bot Messages
机器人消息
Type any text here Add Message BubbleSelect InputText InputButtonsStar RatingsCalendar & TimeImage/File InputSelect User Input:在此处键入任何文本添加消息气泡选择输入文本输入按钮星级评分日历和时间图像/文件输入选择用户输入:
Name
-->姓名
-->
Type Input:类型输入:
File UploadTake PhotoFile Upload & Take PhotoButton文件上传拍照文件上传和拍照按钮
Add ButtonNumber of stars:添加按钮星数:
345Steps in stars:345步星:
0.51Type Input:0.51型输入:
NameFull NameEmailMobilePasswordCustom TextCalendarData Limitation TypeAbsolute DateRelative DateMin DateNameFull NameEmailMobilePasswordCustom TextCalendarData Limitation TypeAbsolute DateRelative DateMin Date
Max Date最大日期
TimeAppointment Duration15 minutes30 minutes1 hour2 hourStart Time时间预约时长15分钟30分钟1小时2小时开始时间
09:00 AM10:00 AM11:00 AM12:00 AMEnd Time09:00 AM10:00 AM11:00 AM12:00 AM结束时间
09:00 AM10:00 AM11:00 AM12:00 AMCancelSave') $("#input_msg").val() $("#input_msg"+num).text(window.localStorage.getItem(key+num)) console.log(count[num-1]) for(var i=2; iType any text here');09:00 AM10:00 AM11:00 AM12:00 AMCancelSave') $("#input_msg").val() $("#input_msg"+num).text(window.localStorage.getItem(key+num)) 控制台.log(count[num-1]) for(var i=2; iType any text here'); $(".container_inside").append(structure);
$(".container_inside").append(结构); $("#add_msg_"+num+i).text(window.localStorage.getItem(num+i));
$("#add_msg_"+num+i).text(window.localStorage.getItem(num+i)); } });
} });
3.views.py 3.views.py
def post_new(request):
if request.method == "POST":
new_poll= text() #call model object
d= request.POST
new_poll.message= d['message']
new_poll.save()
print(new_poll)
return render(request, "loggedin.html")
4.models.py 4.models.py
class text(models.Model):
message = models.TextField(max_length=250)
time = models.DateTimeField(default=datetime.now)
5.urls.py 5.urls.py
path('newpostx/',views.post_new,name='post_new'),
Basically you are trying to send single value, so jQuery maps sent variable name to 'x' (so you get {'x': $('#input_msg').val()}
in request.POST
).基本上你正在尝试发送单个值,所以 jQuery 将发送的变量名映射到'x'(所以你在
request.POST
中得到{'x': $('#input_msg').val()}
)。 To solve that you need to overwrite your data
attribute in AJAX
call.要解决这个问题,您需要在
AJAX
调用中覆盖您的data
属性。
So you change this line:所以你改变这一行:
data: x
In jQuery/JavaScript file to:在 jQuery/JavaScript 文件中:
data: {'message': x}
And it should work.它应该工作。 Also note that your first alert won't work because you change
document.location
and thus you change your current site before executing alert()
call.另请注意,您的第一个警报将不起作用,因为您更改
document.location
,因此您在执行alert()
调用之前更改了当前站点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.