How to create reusable components in django

129 views
Skip to first unread message

Vivek

unread,
Jul 26, 2017, 7:52:54 AM7/26/17
to Django users
Hello Guys
I am new to django. i am working on a web app and one of the requirements in the application is a dropdown list in the header that displays the number of users registered to a company that logins( The list is limited to just 10 users). so this list needs to be there for all the pages which the company has access to.

How do i make this list dynamic. Can i create a reusable component or plugin that will just display this in the layout when i call it.
Is there any other way of displaying this.

Derek

unread,
Jul 26, 2017, 9:25:12 AM7/26/17
to Django users
You can include a template snippet into the body of the main template that is rendering your current view.

That snippet in turn can either be passed data (such as a user count) from the view that calls the main template - see  http://code.runnable.com/UpeQSkq7JxN6AANF/dynamic-content-in-a-template-in-django-for-python (but imagine the datetime.datetime.now() function being replaced by your own)

You can also create your own "tag" which makes the results from a function readily available - this example:
https://stackoverflow.com/questions/40498091/how-do-i-register-a-dynamic-inclusion-tag-within-django
looks like it might help you.
Reply all
Reply to author
Forward
0 new messages