At work, I stumbled upon an except clause with an or operator:
try:
# Do something.
except IndexError or KeyError:
# ErrorHandling
Run Code Online (Sandbox Code Playgroud)
I know the exception classes should be passed as a tuple, but it bugged me that it wouldn't even cause a SyntaxError.
So first I wanted to investigate whether it actually works. And it doesn't.
>>> def with_or_raise(exc):
... try:
... raise exc()
... except IndexError or KeyError:
... print('Got ya!')
...
>>> with_or_raise(IndexError)
Got ya!
>>> …Run Code Online (Sandbox Code Playgroud) 我想更改博客条目的URL,以包括类别的条目(@route(r'^([category-slug]/[post-slug]例如-localost / health / health_blog_1)。
我将如何更改此模型?
class PostPage(RoutablePageMixin, Page):
body = RichTextField(blank=True)
date = models.DateTimeField(verbose_name="Post date", default=datetime.datetime.today)
categories = ParentalManyToManyField('blog.BlogCategory', blank=True)
tags = ClusterTaggableManager(through='blog.BlogPageTag', blank=True)
content_panels = Page.content_panels + [
FieldPanel('body', classname="full"),
FieldPanel('categories', widget=forms.CheckboxSelectMultiple),
FieldPanel('tags'),
]
settings_panels = Page.settings_panels + [
FieldPanel('date'),
]
@property
def blog_page(self):
return self.get_parent().specific
def get_context(self, request, *args, **kwargs):
context = super(PostPage, self).get_context(request, *args, **kwargs)
context['blog_page'] = self.blog_page
return context
Run Code Online (Sandbox Code Playgroud)