Reputation: 1203
I am creating a application using django
My Code as follows:
class Profile(models.Model):
Name = models.OneToOneField(settings.AUTH_USER_MODEL,on_delete=models.CASCADE)
E_mail = models.EmailField(max_length=70)
Phone_no = models.BigIntegerField()
Skills = models.TextField()
Image = models.ImageField(upload_to='user_images')
I want to autogenerate the user email in the user profile which the user had given at the time of signup.
I am using the django autheticated models for signup and login and I am using class based views for my profile generation.
Can anybody help me in this?
Thank you
Upvotes: 0
Views: 71
Reputation: 2940
For this case you can leverage Django's custom context processors for template.
Create a file context_processors.py
in your main app folder which will contain your user object, like this:
def user(request):
return {'user': {'email': request.user.email if request.user.is_authenticated else None}}
Then in settings.py
file add your context processors's name under context_processors
list, like this:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['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',
'myapp.context_processors.user', # This is our custom context processors
],
},
},
]
Now you can access user email in all templates with this syntax,{{ user.email }}
Upvotes: 1