Functions

Learn How to Make Your Own Functionality Using Template Tags

Introduction


The default modules and template tags are included with Donation Store, however you can make your own template tags/add additional functionality using functions/template tags. You can create your own front end HTML that can call backend Python functions. That means you can run normal programming logic and get it returned to your front end webstore.

How to Create a Custom Function


First, create a Python file in the /store/templatetags directory of the application. As it is a Python file, it should follow Standard Python Naming Conventions. It must be snake case (words separated only by underscores), there should be no capital letters, they should all remain lowercase and there should not be any special characters or Hyphens. Note, modules.py is already taken and can't be used as it is used by Donation Store for rendering modules. A valid file name would be

/store/templatetags/my_custom_functions.py

After creating the Python file, you must register that file with Donation Store so you can include it in your templates. At the very start of the file, you need to place the following code

from django import template

register = template.Library()

Once you have done that, you can go ahead and start creating functions that can link to your stores front end. Please note that these function names must be unique and cannot clash with the names already defined in the modules.py file , so please check there. You can also check out the Modules Doc to see the names of the functions.

You can create a function now using

@register.simple_tag(takes_context=True)
def my_new_module(context):
	# Your Python code here.
	return 'Example Content'

Now in your template in the template you want to use this function on, you must import the Python file like this (without the .py extension)

{% load {custom_module_file_name} %}

Using the above example (my_custom_functions.py) with the above defined function, to load it and call the function, you would use

{% load my_custom_functions %}
{% my_new_module as content %}
    {{ content }}

Using the content variable would then display the words Example Content


Have Questions? Open a Support Ticket

View Common Issues on the Knowledgebase

Video Guides on YouTube

Other clients and Donation Store developers hang out on our Discord server, where you can ask for support in #ds-chat, or if you are a Client and you don't uet have your Client role on Discord, let us know and we can add it. Once added you get access to our private Client's support channel.