【问题标题】:Dajax/Dajaxice saving object in ajax.py with parameters带有参数的 ajax.py 中的 Dajax/Dajaxice 保存对象
【发布时间】:2013-01-25 20:23:02
【问题描述】:

我已经完成了设置,并且 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:出了点问题。

如果我遗漏了任何可能重要的内容,请告诉我。

非常感谢,

【问题讨论】:

  • 这可能是由于您创建和传递 JSON 对象的方式所致。请在您的数据对象上尝试 JSON.stringify() 函数 (var data = JSON.stringify($('#comment_form').serialize(true)))。我没有使用过 JQuery,因此我可能会遗漏一些东西,但我有充分的理由怀疑这是由于您创建和传递 JSON 对象的方式造成的。

标签: python django jquery dajaxice dajax


【解决方案1】:

对我来说唯一突出的(而且我不是专家,所以谁知道...)是在您的 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()

【讨论】:

    【解决方案2】:

    发送表单的方式对于 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()
    

    【讨论】:

      【解决方案3】:

      我可以在这里看到一些东西,但看不到 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 using a subset of fields on the form 在这个 dajax 示例中可以更详细地看到 dajax 返回片段:Dajax form validation example

      【讨论】:

        【解决方案4】:

        我找不到让它工作的方法。我认为这是 Dajaxice 的问题,您可以做的是避免 request.POST QueryDict 而是使用 request.raw_post_data。您需要执行与 urlparse 相反的操作:

        data = urlparse.parse_qs(request.raw_post_data)
        

        然后你需要反序列化它。

        data = json.loads(data.get('argv'))
        

        这将返回一个参数列表,使用列表中的第一个元素。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-11-21
          • 1970-01-01
          • 1970-01-01
          • 2012-04-22
          • 2012-04-20
          • 2013-08-29
          • 2013-04-04
          相关资源
          最近更新 更多