Template refactoring

Refactor some parts of the template which were duplicated everywhere:
- Pagination.
- Share buttons: use the same "share" template for all pages, also fix the share links & titles.
- Query generation: avoid string concatenation in templates, and no more encoding by hand!

In these changes, I've tried to better use template tags and template `include` to avoid duplicate code and to move complexity out of templates to Python code.