我们从Python开源项目中,提取了以下4个代码示例,用于说明如何使用django.http.response.HttpResponseServerError()。
def handler500(request, template_name='500.html'): t = get_template(template_name) tt, value, tb = sys.exc_info() ctx = Context({'exception_value': value, 'value': tt, 'tb': traceback.format_exception(tt, value, tb)}) return HttpResponseServerError(t.render(ctx))
def process_exception(self, request, exception): if settings.DEBUG: # Default exception handling, which is already nice when in DEBUG mode. return return HttpResponseServerError( render_to_string( '500.html', { 'exception': exception, 'exception_class': exception.__class__.__name__, 'error_trace': traceback.format_tb(exception.__traceback__, limit=100) } ) )
def health(request: HttpRequest) -> HttpResponse: c = connection.cursor() # type: CursorWrapper try: c.execute("SELECT 1") res = c.fetchone() except DatabaseError as e: return HttpResponseServerError(("Health check failed: %s" % str(e)).encode("utf-8"), content_type="text/plain; charset=utf-8") else: return HttpResponse(b'All green', status=200, content_type="text/plain; charset=utf-8")
def obj_create(self, bundle, **kwargs): if not super(UploadUrlResource, self).is_valid(bundle): raise ImmediateHttpResponse(response=self.error_response(bundle.request, bundle.errors)) log.info('Received request to create a new document upload record.') default_key_pair = get_default_key_pair() if not default_key_pair: err = 'No default key pair defined.' e = Exception(err) e.response = HttpResponseServerError('Internal configuration error: {error}.'.format(error=err)) log.critical(err) raise e symmetric_key = generate_symmetric_key() if log.isEnabledFor(DEBUG): log.debug('Generated symmetric key for request: {key}.'.format( key=symmetric_key )) encrypted_symmetric_key = encrypt(symmetric_key, default_key_pair.public.key) if log.isEnabledFor(DEBUG): log.debug('Encrypted symmetric key for request: {key}.'.format( key=encrypted_symmetric_key )) current_user = bundle.request.user if log.isEnabledFor(DEBUG): log.debug('User for request: {user}.'.format( user=current_user )) document_metadata_map = bundle.data.get('document_metadata', {}) document = self.create_document(current_user, default_key_pair, document_metadata_map, encrypted_symmetric_key) log.info('Document created for request.') single_use_url = sign_url(document.document_id, method=PUT) if log.isEnabledFor(DEBUG): log.debug('Created single use URL for request: {url}.'.format( url=single_use_url )) upload_url = Url(document_id=document.document_id, single_use_url=single_use_url, symmetric_key=symmetric_key) if log.isEnabledFor(DEBUG): log.debug('Generated upload URL {upload_url} for {user}.'.format( upload_url=upload_url, user=current_user.username, )) bundle.obj = upload_url return bundle