Repository: wagtail/wagtail

Issue:
`to_python` throws `AttributeError` on `TableBlock` when value is None
### Issue Summary

Similar to: https://github.com/wagtail/wagtail/issues/4822

After upgrading one of our sites to 6.0, attempting to update some of our pages' indices results in a server error.

```
  File "/venv/lib/python3.8/site-packages/wagtail/contrib/table_block/blocks.py", line 135, in to_python
    if not value.get("table_header_choice", ""):
AttributeError: 'NoneType' object has no attribute 'get'
```

This is because, in Wagtail 6.0, the following code has been added:
https://github.com/wagtail/wagtail/commit/fe1a306285da6c48b66506c589abc2c66b51668a#diff-f058d7a36746222950f678ed6b178205778e4e1e5b85f0299c2a7c3b9d6c8939R129

### Steps to Reproduce

1. Create a page with an empty TableBlock in its body
2. Before Wagtail 6.0, this should save and render OK
3. After upgrading to 6.0, editing the same page will throw an error.
4. Looking at the logs, you should see the error above

A workaround is to identify those pages and remove the empty TableBlocks, but this would prove to be a tedious task if there are many page instances.

A possible fix would be to check if the value is not None first:
```
if value and not value.get("table_header_choice", ""):
```

Discussion:
squash-labs[bot]: <h2>Manage this branch in <a href="https://app.squash.io/accounts/">Squash</a></h2>Test this branch here: <a href='https://tableblock-fix-aqj7b.squash.io' target='_blank'>https://tableblock-fix-aqj7b.squash.io</a><br/>
squash-labs[bot]: <h2>Manage this branch in <a href="https://app.squash.io/accounts/">Squash</a></h2>Test this branch here: <a href='https://laymonagetableblock-fix-i1sdb.squash.io' target='_blank'>https://laymonagetableblock-fix-i1sdb.squash.io</a><br/>

Patch:
diff --git a/wagtail/contrib/table_block/blocks.py b/wagtail/contrib/table_block/blocks.py
index 38066501f2..ace83383c5 100644
--- a/wagtail/contrib/table_block/blocks.py
+++ b/wagtail/contrib/table_block/blocks.py
@@ -132,7 +132,7 @@ class TableBlock(FieldBlock):
         value for the header choice. I would really like to have this default to "" and force the
         editor to reaffirm they don't want any headers, but that would be a breaking change.
         """
-        if not value.get("table_header_choice", ""):
+        if value and not value.get("table_header_choice", ""):
             if value.get("first_row_is_table_header", False) and value.get(
                 "first_col_is_header", False
             ):


Test patch:
diff --git a/wagtail/contrib/table_block/tests.py b/wagtail/contrib/table_block/tests.py
index 51e9eff6dd..98fb8ef074 100644
--- a/wagtail/contrib/table_block/tests.py
+++ b/wagtail/contrib/table_block/tests.py
@@ -439,6 +439,12 @@ class TestTableBlock(TestCase):
         """
         self.assertHTMLEqual(result, expected)
 
+    def test_empty_table_block_to_python(self):
+        # Should not raise an error
+        # See https://github.com/wagtail/wagtail/issues/11816
+        block = TableBlock()
+        self.assertIsNone(block.to_python(None))
+
 
 class TestTableBlockForm(WagtailTestUtils, SimpleTestCase):
     def setUp(self):