Home > Enterprise >  Print users' data inside a template in Django
Print users' data inside a template in Django

Time:02-05

I'm new to Django and I'm trying to print users' information such as email, first name, username in a template that should work as "My Profile Page".

For some reason, the only thing I'm able to print is the username of the logged-in user with {{request.user.username}} and the id (both {{request.user.id}} and {{user.id}} work)

Users are registered using the UserCreationForm

If I try to print the email with {{request.user.email}} or {{user.email}} nothing is shown.

My settings.py is the default one

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR / 'templates']
        ,
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

CodePudding user response:

My assumption that your user email field is not required and it's None that is why it's not printed. You can change it either via admin or via Django shell ./manage.py shell find your user user = User.objects.get(id=your_id) then user.email = '[email protected]' and user.save() and check

  •  Tags:  
  • Related