3

I want to show the user's first name on the navbar of my project. This navbar is on the base.html file.

In my views.py I have created a variable that does this

def base(request):
    user = request.user
    u = User.objects.get(username=user)
    us = u.clients.first_name
    context = {'u': u, 'us': us}
    return render(request, "backend/base.html", context)

I can get it to work on any other .html file, but not on the base.html, that looks like this

<span class="mb-0 text-sm  font-weight-bold">{{us}}</span>

This is my model

class Clients(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    first_name = models.CharField(max_length=30, verbose_name="Primeiro Nome")
    last_name = models.CharField(max_length=30, verbose_name="Apelido")
    address = models.CharField(max_length=200, verbose_name="Morada")
    nif = models.CharField(max_length=9, verbose_name="NIF", validators=[RegexValidator(r'^\d{1,10}$')], primary_key=True)
    mobile = models.CharField(max_length=9, verbose_name="Telemóvel", validators=[RegexValidator(r'^\d{1,10}$')])
    email = models.CharField(max_length=200, null=True, verbose_name="Email")

    def __str__(self):
        return "%s %s" % (self.first_name, self.last_name)

    class Meta:
        verbose_name_plural = "Clientes"
11
  • What is the error message or does it just not render anything? Commented Feb 17, 2020 at 23:28
  • @KentShikama It doesn't render anything. If I use {{user}} it will render the username, but if I use {{us}} (after the variable defined on the views.py) it renders nothing. Commented Feb 17, 2020 at 23:30
  • What is the django version? Commented Feb 17, 2020 at 23:45
  • @felipsmartins 3.0.3 Commented Feb 17, 2020 at 23:48
  • Are you sure you have related Client object ( with same user) for user you are trying to list in database Commented Feb 17, 2020 at 23:57

2 Answers 2

1

I am answering my own question.

I was able to solve this by creating a context_processor.py with the following code

from .models import Clients, Hotels, Trip, Flight
from django.contrib.auth.models import User

def add_variable_to_context(request):  
    user = request.user
    u = User.objects.get(username=user)
    us = u.clients.first_name
    uv = u.clients.avatar
    return {
        'u': u,
        'us': us,
        'uv': uv
    }
Sign up to request clarification or add additional context in comments.

4 Comments

Was typing my answer but there you go, also don't forget to add 'appname.context_processors.add_variable_to_context', in Templates (context_processors)
Great! Might as well mark your own answer as the answer to your question so this question will be closed
@Steven I tried, but it will only let me do it in 2 days.
Well no worries, as long as you've solved your question haha
0

In views.py try to use render_to_string function. And dont forget to configure the static files settings in configuration file.

from django.template.loader import render_to_string

context = {'u': u, 'us': us}
render_to_string('html_name.html', context)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.