我有一个模型,该模型在models.py
中定义如下class Comments(models.Model):
Post_Name = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='Comments_Post_Name')
Comment_By = models.ForeignKey(User, on_delete=models.CASCADE, related_name='Comment_By')
Comment = models.CharField(max_length=100000000, blank=True)
def __str__(self):
return self.Post_Name.Post_Caption + " Commented By " + self.Comment_By.username
我想使用AJAX更改“注释”,但没有更改。 我的AJAX视图和Javascript如下。
def EditComment(request):
ctx = {}
if request.method == 'POST':
print(request.POST)
New = Comments.objects.get(id=request.POST['Comment'])
New.Comment = request.POST['Change']
New.save()
print(New.Comment)
ctx = {'result': 'Done'}
return HttpResponse(json.dumps(ctx), content_type='application/json')
这是调用AJAX的脚本。
function EditComment() {
var str = document.getElementById('Comment_ID').value
var saveChange = document.getElementById('message-text').innerHTML
console.log(str)
console.log(saveChange)
$.ajax(
{
type: 'POST',
url: "{% url 'EditComment' %}",
data: {
'Comment': str,
'Change': saveChange,
'csrfmiddlewaretoken': '{{ csrf_token }}'
},
dataType: 'json',
success: function (response) {
console.log(response)
},
error: function (rs) {
alert(rs.responseText);
}
}
)
}
此外,在终端窗口中,打印了我要编辑的同一注释对象...只是它的内容没有被编辑.....编辑后我也保存了该对象,但仍然没有保存...
任何解决方案?
答案 0 :(得分:0)
我遇到了错误...我在AJAX数据字典中输入了错误的值