django-siteblocks documentation¶
http://github.com/idlesign/django-siteblocks
django-siteblocks is a reusable application for Django to build blocks of static or dynamic data that could be used in templates.
It allows you to describe data that doesn’t clearly belong to any application in your project in terms of static or dynamic blocks, that could be rendered in certain places on site pages. These blocks are addressed in templates by their aliases.
Requirements¶
- Python 3.6+
- Django 2.0+
- Django Admin contrib enabled (optional)
Table of Contents¶
Getting started¶
- Add the siteblocks application to INSTALLED_APPS in your settings file (usually ‘settings.py’).
- Use
> python manage.py migrate
command to install app tables into DB. - Make sure TEMPLATE_CONTEXT_PROCESSORS in your settings file has django.core.context_processors.request if you want to use static blocks created in Django Admin.
For Django 1.8+: django.template.context_processors.request should be defined in
TEMPLATES/OPTIONS/context_processors
.
Quick example¶
Note
This example covers only static siteblocks. More advanced technique can be found in Dynamic siteblocks section.
Let’s say we need random quotes block.
Add {% siteblock “my_quotes” %} tag where you need it in templates. Here
my_quotes
is the alias of a block.{% extends "_base.html" %} {% load siteblocks %} {% block sidebar %} <div class="quote"> {% siteblock "my_quotes" %} </div> {% endblock %}
Note
You can use {% siteblock “my_quotes” as my_var %} tag variation to put block contents into a template context variable.
Go to Django Admin site and add several siteblocks with quotes aliased my_quotes.
Note that you can render different sets of quotes on different pages (URLs or views).
Random quotes are here. You’re done.
Dynamic siteblocks¶
Dynamic siteblocks instead of Static ones allows complex logic to build siteblock contents.
Siteblocks content in this case is not defined using Django Admin contrib, but generated by an ordinary Python function registered as a siteblock.
Use register_dynamic_block() function to register a callable that produces contents for a dynamic block.
Such a callable will get the following kwargs when called:
- block_alias - block alias,
- block_context - template context for block
Let’s implement quotes siteblock similar to that from Getting started, but dynamic this time.
Add {% siteblock “my_quotes” %} tag where you need it in templates. Here
my_quotes
is the alias of a block.{% extends "_base.html" %} {% load siteblocks %} {% block sidebar %} <div class="quote"> {% siteblock "my_quotes" %} </div> {% endblock %}
Define and register a dynamic siteblock.
# Put the following code somewhere where it'd be triggered as expected. E.g. in app view.py. from random import choice # Import the register function. from siteblocks.siteblocksapp import register_dynamic_block # The following function will be used as a block contents producer. def get_quote(**kwargs): quotes = [ # From Terry Pratchett's Discworld novels. 'Ripples of paradox spread out across the sea of causality.', 'Early to rise, early to bed, makes a man healthy, wealthy and dead.', 'Granny had nothing against fortune-telling provided it was done badly by people with no talent for it.', 'Take it from me, there\'s nothing more terrible than someone out to do the world a favour.', 'The duke had a mind that ticked like a clock and, like a clock, it regularly went cuckoo.', 'Most gods find it hard to walk and think at the same time.', 'They didn\'t have to be funny - they were father jokes', 'Speak softly and employ a huge man with a crowbar.', ] return choice(quotes) # And we register our siteblock. register_dynamic_block('my_quotes', get_quote)
Thirdparty applications support¶
Here belongs some notes on thirdparty Django applications support in siteblocks.
django-modeltranslation¶
https://pypi.python.org/pypi/django-modeltranslation/
You can use modeltranslation to make your site blocks language dependent.
This will require setting SITEBLOCKS_I18N_SUPPORT to True in settings.py of your project.
Get involved into django-siteblocks¶
Submit issues. If you spotted something weird in application behavior or want to propose a feature you can do that at https://github.com/idlesign/django-siteblocks/issues
Write code. If you are eager to participate in application development, fork it at https://github.com/idlesign/django-siteblocks, write your code, whether it should be a bugfix or a feature implementation, and make a pull request right from the forked project page.
Translate. If want to translate the application into your native language use Transifex: https://www.transifex.com/projects/p/django-siteblocks/.
Spread the word. If you have some tips and tricks or any other words in mind that you think might be of interest for the others — publish them.
Also¶
If the application is not what you want for user registration, you might be interested in considering the other choices — https://www.djangopackages.com/grids/g/layout/