鱼C论坛

 找回密码
 立即注册
查看: 1360|回复: 2

django ajax提交form表单数据页面跳转问题

[复制链接]
发表于 2020-5-20 13:24:37 | 显示全部楼层 |阅读模式
10鱼币
本帖最后由 kylin121380 于 2020-5-20 13:25 编辑

点击提交后页面页面还是进行了跳转,请问哪里出了问题

html中ajax:
  1.         $("#comment_form").submit(function(){
  2.             // 更新数据到textarea
  3.             CKEDITOR.instances['id_text'].updateElement();

  4.             // 异步提交
  5.             $.ajax({
  6.                 url: "{% url 'comment:comment_sub' %}",
  7.                 type: 'POST',
  8.                 data: $(this).serialize(),
  9.                 cache: false,
  10.                 success: function(data){
  11.                     console.log(data);
  12.                     if(data['status']=="SUCCESS"){
  13.                         // 清空编辑框的内容
  14.                         CKEDITOR.instances['id_text'].setData('');
  15.                     }else{
  16.                         // 显示错误信息
  17.                         $("#comment_error").text(data['message']);
  18.                     }                    
  19.                 },
  20.                 error: function(xhr){
  21.                     console.log(xhr);
  22.                 }
  23.             });
  24.             return false;
  25.         });
复制代码


form:
  1.                 <form id="comment_form" action="{% url 'comment:comment_sub' %}" method="POST">
  2.                         {% csrf_token %}
  3.                         {% for field in comment_form %}
  4.                                 {{field}}
  5.                         {%endfor%}
  6.                         <input type="submit" value="评论" class="btn btn-primary pull-right">
  7.                         <hr>
  8.                 </form>
复制代码


后端views:
  1. @csrf_exempt
  2. def comment_sub(request):
  3.     referer = request.META.get('HTTP_REFERER', '/')  # 跳转回原网页
  4.     comment_form = CommentForm(request.POST, user=request.user)
  5.     data = {}
  6.     if comment_form.is_valid():
  7.         comment = Comment.objects.create(
  8.             article_comment=comment_form.cleaned_data['comment_content'],
  9.             user=comment_form.cleaned_data['user'],
  10.             article=comment_form.cleaned_data['bbs'],
  11.         )
  12.         comment.save()
  13.         # return redirect(referer)
  14.         data['status'] = 'SUCCESS'
  15.         data['user'] = comment.user.username
  16.         data['text'] = comment.article_comment
  17.         data['comment_time'] = comment.date.strftime('%Y-%m-%d %H:%M:%S')
  18.     else:
  19.         # return render(request, '404.html', {'message': comment_form.errors, 'redirect_to': referer})
  20.         data['status'] = 'ERROR'
  21.     return JsonResponse(data)
复制代码

本帖被以下淘专辑推荐:

  • · django|主题: 61, 订阅: 0
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-5-20 21:28:55 | 显示全部楼层
提交后会自动跳转到原本指定页面,页面信息是jsonresponse返回的信息。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-5-20 22:42:53 | 显示全部楼层
textarea的id写错了CKEDITOR.instances['id_text'].updateElement();里面的id改成自己textarea的id就好了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-19 22:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表