【发布时间】:2014-07-28 00:57:11
【问题描述】:
一个页面上有多个 div,每个 div 都有一个表单。我想做一个 AJAX Get,以便将输入到表单中的文本和包含表单的 div 的 id 传递到 Django 视图中,保存到那里的数据库中,然后在类似的 div 中显示文本页面底部。我写了以下代码:
HTML
<div class="jumbotron" id="post #{{post.id}}"><h3>{{post.post}}</h3>
<input type = "text" style="display:none;">{% csrf_token %}</input>
<button type="button" class="btn-primary" id='{{post.id}}'>Add post.</button>
</form>
</div>
scripts.js
$(".btn-primary").click(function(){
var post_id = $(this).attr('id');
var post_text = $(this).parent().children("input").val();
$.get('/makepost/'),{post_id:post_id, post_text:post_text }, function(data){
newsimp_id = "#"+data;
$.scrollTo($(newsimp_id).position().top, 500);
}
});
django 视图
def makepost(request):
context = RequestContext(request)
parent_post_id = int(request.GET['post_id'])
post_text = request.GET['post_text']
parent_post = Post.objects.get(id=parent_post_id)
c = Post.objects.get_or_create(post = parent_post.post, parent_post = parent_post_id, post = post_text, coeficient = parent_post.coeficient + 1)[0]
new_post_id = c.id
return HttpResponse(new_post_id)
这会引发以下错误:
MultiValueDictKeyError at /makepost/
"'post_id'"
post.post 表示模型中名为 post 的 post 字段。
我该怎么办?这个错误是什么意思?有其他选择吗?
【问题讨论】:
-
您需要显示错误的更多详细信息。您没有在您发布的代码中的任何地方使用“simple_id”键:您应该显示实际代码和完整的回溯。
-
@DanielRoseman 对不起。那是另一个页面的错误。发错了。现在编辑了。
标签: javascript jquery html ajax django