我正在尝试使用ajax提交登录表单.我很困惑,我应该如何处理异常/成功的回应.我从服务器获得200 OK,表单通过密码/用户名字段返回错误.如何根据服务器响应获取显示或将用户重定向到相应页面的错误消息?
JQUERY:
56 $(window).load(function(){
57 $('#login_form').submit(function(e){
58 e.preventDefault();
59 var request_url = document.getElementById('next').value
60 $.ajax({
61 type:"POST",
62 url: $(this).attr('action'),
63 data: $('#login_form').serialize(),
64 success: function(response){ $('#msg').text(response);
65 console.log(response);
66 },
67 error: function(xhr, ajaxOptions, thrownError){ alert( $('#login_error').text('Username already taken. Please select another one.')},
68 });
69 });
70 });
Run Code Online (Sandbox Code Playgroud)
查看:更新
51 def login(request):
52 if request.method == 'POST':
53 request.session.set_test_cookie()
54 login_form = AuthenticationForm(request, request.POST)
55 if login_form.is_valid():
56 if request.is_ajax:
57 user = django_login(request, login_form.get_user())
58 if …Run Code Online (Sandbox Code Playgroud) 有可能使用Django的ORM以编程方式连接两个表吗?我有两个模型:主题和投票.在我的模板上,我有一个用户可以上/下投票的主题列表,如Reddit.除了对结果进行排序外,一切都正常.我无法弄清楚如何根据得分对对象列表进行排序,该得分是每个对象的投票计数的总和.我可以毫无问题地从postgres中检索所需的数据:
select i.id, i.title, i.date_created, s.object_id, s.vote, Sum(vote)
from topic_topic i, votes s
where i.id = s.object_id
group by 1, 2, 3, 4, 5
order by sum DESC;
Run Code Online (Sandbox Code Playgroud)
它返回所需的结果:
id | title | date_created | object_id | vote | sum
11 | sdfg | 2012-06-04 23:30:17.805671-07 | 11 | 1 | 2
1 | test | 2012-05-13 17:03:24.206092-07 | 1 | 1 | 2
3 | asdf | 2012-05-13 19:23:15.059135-07 | 3 | 1 | 2
2 | adsf | …Run Code Online (Sandbox Code Playgroud)