Search code examples
djangodjango-templatesdjango-registration

Defining template variables in django-registration


I am trying pass a new variable into a template within django-registration. Here is the code I have --

# in the template:

<table>
{% for user in user_list %}
<tr>
    <td>{{ user.username }}</td>

</tr>
{% endfor %}
<table>

Where would I put the following user_list definition?

from django.contrib.auth.models import User
'user_list':User.objects.all()

Solution

  • you can append a new method to TEMPLATE_CONTEXT_PROCESSORS in settings.py, e.g.

     #setting.py
        TEMPLATE_CONTEXT_PROCESSORS = (
        'other_contexts',
        'yourproject.yourcontextfile.yourcontextmethod',
          )
    

    and then in your yourcontextfile.py write yourcontextmethod like this:

    from django.contrib.auth.models import User
    
        def yourcontextmethod(request):
            return {'user_list':User.objects.all()}