小编Loï*_*ira的帖子

Why does using `or` within an except clause not cause a SyntaxError? Is there a valid use for it?

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)

python error-handling

11
推荐指数
1
解决办法
141
查看次数

使用RoutablePageMixin基于的Wagtail自定义URL

我想更改博客条目的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)

django wagtail

2
推荐指数
1
解决办法
443
查看次数

标签 统计

django ×1

error-handling ×1

python ×1

wagtail ×1