당신은 POST에 자바 스크립트와 (당신은 곧 예에서와 같이 jQuery를를 사용하는 경우 더 쉽게) 입력 상자의 텍스트를 XMLHttpRequest의 (https://developer.mozilla.org/en/using_xmlhttprequest)를 수행하고, DB 쿼리 서버 측 작업을 수행 할 수 있습니다 이메일 (입력란 텍스트)이 고유한지 확인하십시오. 따라서 IE 브라우저가 아닌 경우 @view_config()
데코레이터의 경우 xhr=True
이고 IE 브라우저의 경우 request.response.content_type='text/html'
이 설정된보기에서 응답을 반환합니다. 예를 들어 :
@view_config(permission='view', route_name='check_email', renderer='json') ##for IE browsers
@view_config(permission='view', route_name='check_email', renderer='json', xhr=True) ##for non-IE
def check_email(request):
email= request.POST['email']
dbquery = DBSession.query(User).filter(User.email==email).first()
## if the email exists in the DB
if dbquery:
msg = 'used'
## else if the email is available
else:
msg = 'available'
if request.is_xhr:
return {'msg':msg}
else: # for IE browser
request.response.content_type = 'text/html'
return Response(json.dumps({'msg':msg}))
당신은 XMLHttpRequest를 돌봐 jQuery와 같은 라이브러리를 사용하여 쉽게 (클라이언트 측)를 POST 할 수 있습니다. 의 .js 당신과 함께 스크립트 파일뿐만 아니라, 템플릿에서 jQuery 라이브러리를 포함한 후 : myscript.js에 다음
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script type='text/javascript' src="{{request.static_url('tutorial:static/myscript.js')}}"></script>
그리고 작업을 수행합니다
$(function() {
$('#email').on('blur', postEmail)
})
// this function POSTs the entered email
function postEmail() {
var email = $(this).val()
$.post('/check_email', email, callback)
}
// this function is used to do something with the response returned from your view
function callback(data) {
if (data['msg'] === 'used') {
alert('used')
}
else if (data['msg'] === 'available') {
alert('available')
}
}
출처
2012-07-03 22:59:43
Raj
감사합니다. 좋은 ... 알고 있습니다. 내 현재 접근 방식은 이메일을 기본 키로 설정 한 다음 'IntegrityError 제외 :'오류를 잡는 것입니다.하지만 사용자에게 미리 경고하는 방법이 있을지도 모릅니다. – Lostsoul
실제로 양식의 모든 값을 저장하고 렌더러에 모든 것을 담은 표제를 제공하여 양식에 정확하게 채워진 부분을 응답 할 때 자동으로 채울 수 있습니다. – Wiz
그 생각을하지 않았지만, 기존의 코드에 적용 할 것이지만 여전히 실시간 피드백을주고 싶다. (내 사이트의 다른 많은 영역에도 적용될 수 있기 때문에). 내 양식이 공백으로 돌아가는 이유와 다른 사람들이 어떻게 가치를 지키는 지 궁금해하십니다. 감사 – Lostsoul