Link
Embed
Share
Beginning
This slide
Copy link URL
Copy link URL
Copy iframe embed code
Copy iframe embed code
Copy javascript embed code
Copy javascript embed code
Share
Tweet
Share
Tweet
Slide 1
Slide 1 text
CBV based navigation Danilo Bargen Djangocon Zürich, 2012/06/05
Slide 2
Slide 2 text
Setup $ pip i ns t a ll django−tabination
Slide 3
Slide 3 text
Creating views from tabination . views import TabView class SpamTab(TabView ) : _is_tab = True tab_id = ’spam’ tab_group = ’mainnav ’ tab_label = ’Spam’ template_name = ’spam. html ’
Slide 4
Slide 4 text
Another view class HamTab(TabView ) : _is_tab = True tab_id = ’ham’ tab_group = ’mainnav ’ tab_label = ’Ham’ template_name = ’ham. html ’
Slide 5
Slide 5 text
urls.py urlpatterns = patterns ( ’ ’ , url ( r ’^spam/$ ’ , views .SpamTab. as_view ( ) ) , url ( r ’^ ham/$ ’ , views .HamTab. as_view ( ) ) , )
Slide 6
Slide 6 text
Spam Template
Slide 7
Slide 7 text
Ham Template
Slide 8
Slide 8 text
Navigation Template blocks/tabnavigation.html {{ tabs }} and {{ current_tab_id }}
Slide 9
Slide 9 text
CSS #tab_navigation l i . active a { font−weight : bold ; color : red ; }
Slide 10
Slide 10 text
Result
Slide 11
Slide 11 text
DRY? class SpamTab(TabView ) : _is_tab = True tab_id = ’spam’ tab_group = ’mainnav ’ tab_label = ’Spam’ template_name = ’spam. html ’ class HamTab(TabView ) : _is_tab = True tab_id = ’ham’ tab_group = ’mainnav ’ tab_label = ’Ham’ template_name = ’ham. html ’
Slide 12
Slide 12 text
DRY! class BaseTab(TabView ) : tab_group = ’mainnav ’ class SpamTab(BaseTab) : _is_tab = True tab_id = ’spam’ tab_label = ’Spam’ template_name = ’spam. html ’ class HamTab(BaseTab) : _is_tab = True tab_id = ’ham’ tab_label = ’Ham’ template_name = ’ham. html ’
Slide 13
Slide 13 text
Extending BaseTab class BaseTab(TabView ) : tab_group = ’mainnav ’ tab_classes = [ ’main−navigation−tab ’ ] def get_context_data ( self , **kwargs ) : context = super(BaseTab, self ) . get_context_data (**kwargs) context [ ’spam’ ] = "SPAM −EVERYWHERE! " return context
Slide 14
Slide 14 text
Visibility control class HiddenTab(BaseTab) : _is_tab = True tab_id = ’hidden ’ template_name = ’hidden . html ’ # look mah, no tab_label !
Slide 15
Slide 15 text
Visibility control class SecretTab(BaseTab) : _is_tab = True . . . @property def tab_visible ( self ) : current_tab = self . current_tab user = current_tab . request . user return user . is_authenticated ()
Slide 16
Slide 16 text
Nice things Implicit registry Extensible Flexible Mixins
Slide 17
Slide 17 text
Not so nice things Metaclass magic CBVs, Complexity Other tab classes are instantiated but not dispatched Currently no support for nested tabs
Slide 18
Slide 18 text
Ugly things Ticket #16074: Generic mixins won’t work before Django 1.5
Slide 19
Slide 19 text
Thanks Github: http://github.com/dbrgn/django-tabination Docs: http://django-tabination.readthedocs.org/ Slides: https://speakerdeck.com/dbrgn/django-cbv-based- navigation Twitter: @dbrgn Github: @dbrgn