在ajax.py中使用参数保存Dajax/Dajaxice对象
我已经完成了设置,并且在dajaxproject.com上的所有示例都运行得很好,但现在我在更复杂的用例中遇到了问题。我想把几个参数传递给ajax函数,同时还想把表单中的文本一起传过去,然后用这些数据创建一个对象。
如果有人能帮我一下,我会非常感激。
我正在使用jquery和jquery.ba-serializeobject.min.js。
Ajax.py:
@dajaxice_register
def save_comment(req, form, user_username, other_username):
dajax = Dajax()
comment_form = CreateCommentForm(deserialize_form(form))
if comment_form.is_valid():
text = comment_form.cleaned_data['text']
user = User.objects.get(username=user_username)
other_user = User.objects.get(username=other_username)
other_profile = Profile.objects.get(user=other_user)
comment = other_profile.comments.objects.create(owner=user, text=text)
comment.save()
return dajax.json()
JS:
<script type="text/javascript">
function add_comment(){
var user = '{{ person.user.username }}';
var other = '{{ other.user.username }}';
var data = $('#comment_form').serialize(true);
Dajaxice.profiles.save_comment(Dajax.process, {'form': data, 'user_username': user, 'other_username': other });
return false;
}
</script>
HTML:
<div><h4>Post Comment:</h4>
<div id="comment_form_errors"></div>
<form action="" id="comment_form" accept-charset="utf-8" method="post">
{% csrf_token %}
{{ commentform.as_p }}
<p><input class="btn profile-comment-submit" id="submit_profile_comment" onclick="add_comment()" type="submit" alt="register" /></p>
<form>
</div>
在Chrome的调试控制台中,我唯一收到的错误是Dajaxice: 出了点问题。
如果我遗漏了什么重要的内容,请告诉我。
非常感谢,
4 个回答
0
这里有几个我能看到的点,不过因为我看不到CreateCommentForm()
和它所创建的模型,所以有些内容可能只是我的猜测。此外,我还假设表单的序列化没有问题。
@dajaxice_register
def save_comment(req, form, user_username, other_username):
dajax = Dajax()
user = User.objects.get(username=user_username)
other_user = User.objects.get(username=other_username)
other_profile = Profile.objects.get(user=other_user)
# Required fields of a form must be checked before calling is_valid() or is_valid fails.
comment = Comments(owner=user)
comment_form = CreateCommentForm(deserialize_form(form), instance=comment)
if comment_form.is_valid():
comment_form.save()
dajax.alert('Form is valid')
else:
dajax.alert('Form is invalid')
for error in comment_form.errors:
dajax.add_css_class('#id_%s' % error, 'error')
# Dajax needs something added to it before the dajax.json() can be returned.
return dajax.json()
关于表单的部分,可以参考这里:Django使用表单的部分字段,而dajax返回的内容可以在这个dajax示例中更详细地查看:Dajax表单验证示例
0
我注意到的唯一一点(我不是专家,所以谁知道呢……)是在你的 ajax.py 文件里。我觉得应该是:
@dajaxice_register
def save_comment(req, form, user_username, other_username):
dajax = Dajax()
comment_form = CreateCommentForm(deserialize_form(form))
if comment_form.is_valid():
text = comment_form.cleaned_data['text']
user = User.objects.get(username=user_username)
other_user = User.objects.get(username=other_username)
other_profile = Profile.objects.get(user=other_user)
comment = Comment(owner=user, text=text)
comment.save()
other_profile.comments.add(comment)
# I don't think you need to other_profile.save(), but you can if you want
return dajax.json()
0
你发送表单的方式对Dajax的工作非常重要。我成功地使用了这个链接和以下的JavaScript代码:
jQuery('form').submit(function(e) {
e.preventDefault()
var data = jQuery(this).serializeObject();
Dajaxice.app.app_name.function_name(Dajax.process,{'form':data});
return false;
});
我看不到你的表单,所以很难全面了解问题。不过我建议你创建一个叫做CommentForm的表单,并在表单初始化时把用户和其他用户的信息放在隐藏字段里。这样代码会简单很多。
你的保存功能就会变得相当简单:
@dajaxice_register
def function_name(request, form):
dajax = Dajax()
form = CommentForm(form)
if form.is_valid():
form.save()
return dajax.json()