Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4587800
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T21:49:55+00:00 2026-05-21T21:49:55+00:00

I am trying to pass the user objects to my template via a templatetag.

  • 0

I am trying to pass the user objects to my template via a templatetag. I first tried simple_tag but apparently it is only for strings? Anyway this is what I have so far:

templatetags/profiles.py

from django.template import Library, Node, Template, VariableDoesNotExist, TemplateSyntaxError, \
                            Variable
from django.utils.translation import ugettext as _
from django.contrib.auth.models import User
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.db import models



class userlist(Node):
    def __init__(self, format_string):
        self.format_string = format_string
    def render(self, context):
        try:
        users = self.format_string
        return users
        except VariableDoesNotExist:
            return None


def get_profiles(parser, token):
    return userlist(User.objects.all())

register = Library()
register.tag('get_profiles', get_profiles)

This is what I have in my template to test it:

{% load profiles %} 
{% get_profiles %}
{% for p in get_profiles %} {{ p }} {% endfor %}

I only get [, , , , ] printed out or if I change User.objects.all() to User.objects.count() I get the correct number. The for iteration in my template doesn’t seem to do anything. what is wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T21:49:56+00:00Added an answer on May 21, 2026 at 9:49 pm

    What is format string? You need to call the template tag like this:

    {% get_all_users as allusers %}
    {% for user in allusers %}
         {{ user.first_name }}
    {% endfor %}
    

    So you need a template tag like

    class GetAllUsers(Node):
        def __init__(self, varname):
            # Save the variable that we will assigning the users to
            self.varname = varname
    def render(self, context):
            # Save all the user objects to the variable and return the context to the template
            context[self.varname] = User.objects.all()
            return ''
    
    @register.tag(name="get_all_users") 
    def get_all_users(parser, token):
        # First break up the arguments that have been passed to the template tag
        bits = token.contents.split()
        if len(bits) != 3:
            raise TemplateSyntaxError, "get_all_users tag takes exactly 2 arguments"
        if bits[1] != 'as':
            raise TemplateSyntaxError, "1st argument to get_all_users tag must be 'as'"
        return GetAllUsers(bits[2])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to pass a dynamic, user created object through AJAX to some C#.
I'm trying to pass the user on to the interface based on their cell
Trying to pass in a user supplied string which has a path to the
I'm trying to pass a variable through a ssh connection, like this: working_dir=/home/user/some_dir/ ssh
I am trying to programatically validate a user login/pass using Spring Security, so I
I am trying pass a new variable into a template within django-registration. Here is
Here's my form i am trying to user to pass two char fields and
I am trying pass class as value in hashmap. I need to get the
Trying to pass something to something else. Says undefined. Not sure how or why.
Trying to pass a string argument to a function, which will then be used

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.