我看到已发布类似的错误here,但这无助于解决我的问题。除了OP没有提供任何有关他在遇到错误时所做的事情的信息(正如其他人在评论中所说)。
我想做什么?
我只是想用ajax将用户注册到Django。
有什么问题?
我在提交表单时出现以下错误:
[15/Jan/2018 17:49:37] "POST /authenticate/register/ HTTP/1.1" 200 14
Traceback (most recent call last):
File "C:\Python27\lib\wsgiref\handlers.py", line 86, in run
self.finish_response()
File "C:\Python27\lib\wsgiref\handlers.py", line 128, in finish_response
self.write(data)
File "C:\Python27\lib\wsgiref\handlers.py", line 212, in write
self.send_headers()
File "C:\Python27\lib\wsgiref\handlers.py", line 270, in send_headers
self.send_preamble()
File "C:\Python27\lib\wsgiref\handlers.py", line 194, in send_preamble
'Date: %s\r\n' % format_date_time(time.time())
File "C:\Python27\lib\socket.py", line 328, in write
self.flush()
File "C:\Python27\lib\socket.py", line 307, in flush
self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 10053] An established connection was aborted by the software in your host machine
[15/Jan/2018 17:49:37] "POST /authenticate/register/ HTTP/1.1" 500 59
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 54896)
Traceback (most recent call last):
File "C:\Python27\lib\SocketServer.py", line 596, in process_request_thread
self.finish_request(request, client_address)
File "C:\Python27\lib\SocketServer.py", line 331, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "C:\Python27\lib\SocketServer.py", line 654, in __init__
self.finish()
File "C:\Python27\lib\SocketServer.py", line 713, in finish
self.wfile.close()
File "C:\Python27\lib\socket.py", line 283, in close
self.flush()
File "C:\Python27\lib\socket.py", line 307, in flush
self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 10053] An established connection was aborted by the software in your host machine
我的代码:
HTML:
<div class="modal animated" id="signupModal" data-backdrop="static" data-keyboard="false">
<div class="modal-dialog modal-sm">
<form id='registerForm'>
{% csrf_token %}
<div class="modal-content">
<div class="modal-header">
<button class="close">×</button>
</div>
<div class="modal-body">
<input class="form-control" type="email" id="inputEmail">
<input class="form-control" type="text" id="inputUsername">
<input class="form-control" type="password" id="inputPassword">
<input class="form-control" type="password" id="inputCPassword">
</div>
<div class="modal-footer" id="signup-footer">
<button type="submit" class="btn btn-default"> Register </button>
</div>
</div>
</form>
</div>
</div>
的Ajax:
$("#registerForm").on('submit', function(e) {
e.preventDefault();
$.ajax({
type: 'POST',
url: "/authenticate/register/",
data: {
'email' : $('#inputEmail').val(),
'username': $('#inputUsername').val(),
'password': $('#inputPassword').val(),
csrfmiddlewaretoken: $('input[name=csrfmiddlewaretoken]').val()
},dataType: 'json',
success: function(data){
if (data['errMsg'] != '')
alert(data['errMsg']);
else
alert('Sending confirmation link...');
}, error: function(xhr){
alert('error!');
}
});
});
views.py:
class Register(View):
form_class = signupForm
def post(self, request):
data = {'errMsg': ''}
form = self.form_class(request.POST)
if form.is_valid():
user = form.save(commit=False)
user.is_active = False
to_email = form.cleaned_data['email']
username = form.cleaned_data['username']
password = form.cleaned_data['password']
user.set_password(password)
user.save()
# code to send confirmation email this code is big
else:
data = { 'errMsg': 'Something went wrong.' }
return JsonResponse(data)
奇怪的是,即使出现该错误,用户也会注册。但为什么我会收到此错误以及如何解决此问题。
答案 0 :(得分:0)
终于找到了解决方案:
我刚刚将form
标记移到<div class="modal-content">
内一步,然后就可以了。 :)
解决方案:
<div class="modal animated" id="signupModal" data-backdrop="static" data-keyboard="false">
<div class="modal-dialog modal-sm">
<!-- from here to -->
<div class="modal-content">
<form id='registerForm'>{% csrf_token %} <!-- to here -->
<div class="modal-header">
<button class="close">×</button>
</div>
<div class="modal-body">
<input class="form-control" type="email" id="inputEmail">
<input class="form-control" type="text" id="inputUsername">
<input class="form-control" type="password" id="inputPassword">
<input class="form-control" type="password" id="inputCPassword">
</div>
<div class="modal-footer" id="signup-footer">
<button type="submit" class="btn btn-default"> Register </button>
</div>
</form>
</div>
</div>
</div>