[英]Dajax/Dajaxice saving object in ajax.py with parameters
我已經完成了安裝過程,並且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:出了點問題。
如果我遺漏了任何可能重要的內容,請告訴我。
非常感謝,
對我而言,唯一引人注意的東西(而且我也不是專家,所以誰知道...)在您的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()
您發送表單的方式對於Dajax至關重要。 我已經成功使用http://benalman.com/projects/jquery-misc-plugins/#serializeobject和以下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並在表單初始化的隱藏字段中填充user和other_user。 這樣可以減少代碼的復雜度
保存功能將非常簡單:
@dajaxice_register
def function_name(request, form):
dajax = Dajax()
form = CommentForm(form)
if form.is_valid():
form.save()
return dajax.json()
我在這里可以看到一些內容,但是看不到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形式驗證示例
我發現沒有辦法使它起作用。 我認為這是Dajaxice的問題,您可以做的是避免使用request.POST QueryDict,而使用request.raw_post_data。 您需要做與urlparse相反的操作:
data = urlparse.parse_qs(request.raw_post_data)
然后,您需要反序列化它。
data = json.loads(data.get('argv'))
這將返回一個參數列表,使用列表中的第一個元素。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.