1

セットアップを実行し、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

4 に答える 4

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()
于 2013-02-19T18:01:08.237 に答える
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 フォーム検証の例

于 2013-02-23T03:33:39.820 に答える
0

フォームの送信方法は、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()
于 2013-02-19T21:13:27.037 に答える