1

I currently have a function called "copyright" (a dynamic copyright message) that I am trying to include into my base Django template, like below:

def copyright():
   some code
   some more code
   print(finaloutput)

I have it sitting in my modules/utils.py which is in my assets directory which I have registered in my static directories.

I want to be able to call that function like {{ copyright }} straight in my top level base.html inside my main templates folder.

I have tried everything to ensure I am loading the staticfiles with no luck. Am I approaching this the wrong way?

Blueliner
  • 23
  • 4

2 Answers2

2

Unfortunately almost everything you're doing here is wrong.

This has nothing to do with static files: as you said yourself, this is a dynamic function so isn't static by definition. Anyway, you can't put Python code in your assets directory. And finally, any function like this will always need to return the result, not print it.

What you need here is a template tag, which you put in your app's templatetags directory and register via the decorator:

@register.simple_tag
def copyright():
   some code
   some more code
   return finaloutput

Then, load the tags in your template and call it as a tag, not a variable:

{% load utils %}  # or whatever you called the file
...
{% copyright %}

See the template tags docs.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Many thanks for pointing me in the right direction. I was able to use your example and get it to work great. Just an oddity for me coming from other languages and learning this framework. I ended up creating an app simply called "tags" just so I could house the templatetags folder and register the app and tags alike as the documentation stated. Before, I was trying to keep the tags out of an "app" folder simply because in my head, the logic should have them reside above the apps where the base html was. But nonetheless, this works fantastic. Thanks! – Blueliner Dec 26 '18 at 04:19
0

There are several ways to achieve your end goal, but nothing you are doing will get you there.

You can,

  1. Use template tags.
  2. Use context processors, in several different ways.
  3. Use {{ view.function_name }} as-is in your templates if you are using class based generic views from Django.

Judging from how I think you have things set up, the fastest way could be to just pass in some context data in your views.

If you are using functional views, your code can look something like this:

def my_view(request):
    def copyright():
        return "copyright 2018"

    return render('my_template.html', {'copyright': copyright})

If you are using class based generic views, you can simply modify your get_context_data.

class Home(TemplateView):
    def get_context_data(self, *args, **kwargs):
        ctx = super(TemplateView, self).get_context_data(self, *args, **kwargs)
        ctx['copyright'] = self.copyright()
        return ctx

    def copyright(self):
        return "copyright 2018"
Navid Khan
  • 979
  • 11
  • 24