Reputation: 39
how can I access to first_name of a user in django template?. for example my writer in django model is like this below :
writer = models.ForeignKey(User, on_delete=models.CASCADE)
Upvotes: 0
Views: 269
Reputation: 353
I have somewhat of a similar setup and this works for me:
models.py
class Customers(models.Model):
name = models.CharField(("Customer name"), max_length=200)
class Licenses(models.Model):
customer = models.ForeignKey(Customers, on_delete=models.CASCADE)
views.py
def licenses (request):
lic = Licenses.objects.all()
return render(request, 'licenses.html',{'lic': lic})
licenses.html
{% for license in lic %}
{{ license.customer.name }}
{% endfor %}
Upvotes: 0
Reputation: 168957
If User
is the Django default user model, it's simply
writer.first_name
-- e.g. if your example is in a post
model you're rendering,
{{ post.writer.first_name }}
in a template.
Upvotes: 1