我们从Python开源项目中,提取了以下8个代码示例,用于说明如何使用wagtail.wagtailcore.models.Page.DoesNotExist()。
def render(self, props): link_type = props.get('linkType', '') if link_type == 'page': try: page_id = props.get('id') page = Page.objects.get(id=page_id) href = page.url except Page.DoesNotExist: href = props.get('url', MISSING_RESOURCE_URL) else: href = props.get('url', MISSING_RESOURCE_URL) anchor_properties = { 'className': 'button', 'href': href, } return DOM.create_element('a', anchor_properties, props['children'])
def Link(props): link_type = props.get('linkType', '') title = props.get('title') if link_type == 'page': try: page_id = props.get('id') page = Page.objects.get(id=page_id) href = page.url except Page.DoesNotExist: href = props.get('url', MISSING_RESOURCE_URL) else: href = props.get('url', MISSING_RESOURCE_URL) anchor_properties = { 'href': href } if title is not None: anchor_properties['title'] = title return DOM.create_element('a', anchor_properties, props['children'])
def Image(props): """ Inspired by: - https://github.com/torchbox/wagtail/blob/master/wagtail/wagtailimages/rich_text.py - https://github.com/torchbox/wagtail/blob/master/wagtail/wagtailimages/shortcuts.py - https://github.com/torchbox/wagtail/blob/master/wagtail/wagtailimages/formats.py """ image_model = get_image_model() alignment = props.get('alignment', 'left') alt_text = props.get('altText', '') try: image = image_model.objects.get(id=props['id']) except image_model.DoesNotExist: return DOM.create_element('img', {'alt': alt_text}) image_format = get_image_format(alignment) rendition = get_rendition_or_not_found(image, image_format.filter_spec) return DOM.create_element('img', dict(rendition.attrs_dict, **{ 'class': image_format.classnames, 'src': rendition.url, 'alt': alt_text, }))
def create_from_sequence(bits): """ Create categories from an iterable """ if len(bits) == 1: # Get or create root node name = bits[0] try: # Category names should be unique at the depth=2 root = Category.get_root_nodes().get(title=name) except Category.DoesNotExist: root = Category.add_root(title=name) except Category.MultipleObjectsReturned: raise ValueError(( "There are more than one categories with name " "%s at depth=1") % name) return [root] else: parents = create_from_sequence(bits[:-1]) parent, name = parents[-1], bits[-1] try: child = parent.get_children().get(title=name) except (Page.DoesNotExist, Category.DoesNotExist): child = parent.add_child(title=name) except Category.MultipleObjectsReturned: raise ValueError(( "There are more than one categories with name " "%s which are children of %s") % (name, parent)) parents.append(child) return parents
def expand_db_attributes(attrs, for_editor): try: page = Page.objects.get(id=attrs['id']).specific if for_editor: editor_attrs = 'data-linktype="page" data-id="%d" ' % page.id else: editor_attrs = '' return '<a %shref="%s">' % (editor_attrs, escape(page.url)) except Page.DoesNotExist: return "<a>"
def Document(props): document_model = get_document_model() try: doc = document_model.objects.get(id=props['id']) doc_meta = get_document_meta(doc) except (document_model.DoesNotExist, AttributeError): return DOM.create_element( 'a', {'href': MISSING_RESOURCE_URL, 'class': MISSING_RESOURCE_CLASS + ' file'}, props['children'] ) icon_element = DOM.create_element(Icon, {'name': doc_meta['extension']}) metadata_element = DOM.create_element( 'span', {'class': 'icon-text__text'}, props['children'], ' ' ) size_element = DOM.create_element( 'span', {'class': 'file-size'}, '({ext} {size})'.format(size=doc_meta['size'], ext=doc_meta['extension'].upper()) ) link_item = DOM.create_element('a', {'href': doc.url, 'class': 'icon-text'}, icon_element, metadata_element) return DOM.create_element('span', {'class': 'file'}, link_item, size_element)
def _create(cls, *args, **kwargs): try: root = Page.objects.get(depth=0) except Page.DoesNotExist: root = Page.add_root(title='root') return root.add_child(title=kwargs['title'])