Reputation: 307
I have a database like this:
from django.db import models
class User(models.Model):
firstName = models.CharField(max_length=30, blank=False, default=None)
lastName = models.CharField(max_length=30, blank=False, default=None)
email = models.EmailField(max_length=100, blank=False, default=None)
password = models.CharField(max_length=100, blank=False, default=None)
score = models.IntegerField(default=0)
in views.py
I want to send a user
that currently logged in and show its informations from DB at template.
my views.py
:
user = User.objects.filter(email=request.POST['email'])
#some code ....
context = RequestContext(request, {
'user': user[0]
})
return HttpResponse(template.render(context))
my template :
{% if user %}
Wellcome {{ user.firstName }}
{% endif %}
but I don't see anything after welcome
.also when I use this:
{% if user %}
Wellcome {{ user }}
{% endif %}
I see welcome anonymousUser
where I am wrong ?
Upvotes: 0
Views: 60
Reputation: 99660
You cannot use user
as the context variable, as it conflicts with the user
object that is injected into the context by the processor
django.contrib.auth.context_processors.auth
Now, to fix your issue, rename user
to user_obj
or something which makes more sense.
Read more here
Upvotes: 2
Reputation: 351
Based on the behavior you are describing you are likely not getting any objects returned from the call to filter:
user = User.objects.filter(email=request.POST['email'])
I would look at the value that is returned from request.POST['email']
and making sure that value is in your datbase as the starting point.
Also, you should be aware that the filter function returns a QuerySet and not a User object. If you want to retrieve a unique User object you can use the get function instead.
Upvotes: 1