1

Before I start, I'd like to point out that I have already surfed half of the internet in search of a possible solution, but nothing really solved my problem. That said, I'm quite the beginner in Django and Python so this could be a simple problem and maybe I'm just too dumb to figure it out.

So, here's the thing. I'm working on a project for college and I have to make a website that emphasises primarily backend. I chose Django as a framework and so far I've been able to have new users register and also log in and out. The problem is maybe not dramatic, but annoying for me at least. To be more clear, After a user logs in, I want to show his name in the top bar alongside the logout button.

So, the log in panel is as such:

LoginPanel

This part is fine, I just hit Log In and it works.

Then I get here:

LoginUser

As you can see, at first, the username is displayed in the top bar alongside the logout. But if I click on "Whatever" or the username itself, it goes away. This may have something to do with the fact that initially, when the username is still visible, I'm in the url "login_user", which in urls.py uses the view "login_user". Once I click on either of those mentioned above, I wanted it to switch to a template called "user_panel.html", but I guess that could be a problem as well. Here is how it looks:

UserPanel

The view "login_user" looks like this:

def login_user(request):

if request.method == "POST":
    username = request.POST['username']
    password = request.POST['password']
    user = authenticate(username=username, password=password)

    if user is not None:
        if user.is_active:
            login(request, user)
            return render(request, 'main/user_panel.html')

        else:
            return render(request, 'main/login.html', {'error_message': 'Your account has been disabled'})
    else:
        return render(request, 'main/login.html', {'error_message': 'Invalid login'})
return render(request, 'main/login.html')

The username is displayed in "user_panel.html" with "{{ user.get_username }}".

Like I said, this could be a really elementary problem, but I simply can't get my head around it, really beats me. Thank you in advance for any possible help.

EDIT:

In settings.py:

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [],
    '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',
        ],
    },
},

]

So, user.get_username is used in user_panel.html like this:

<li>
  <a href="{%url 'main:user_panel'%}">
    <div style="font-size:1.5em; color:lightblue">
     <i class="fas fa-user-circle"></i> <font size="5" face = "Ubuntu" color="white">{{ user.get_username }}</font></div>
  </a>
</li>

user_panel view:

def user_panel(request):
template = loader.get_template('main/user_panel.html')

return HttpResponse(template.render())

Now it shows the username, but everytime I click, downloads something:

Downloads

10 Rep
  • 2,217
  • 7
  • 19
  • 33
CatalinC
  • 77
  • 10
  • Can you add your `TEMPLATES` variable from the settings to the question? The current answers aren't necessarily "wrong", but I don't think it should be necessary to do anything to get the user into the template, with the default `TEMPLATES` set when you start a Django project. – Paulo Almeida Mar 12 '18 at 18:59
  • I just edited it. – CatalinC Mar 12 '18 at 19:03
  • That's what I have in a recently created project, and `{{ user }}` works without any other configuration. Can you show the template, or at least the part where `{{ user.get_username }}` is being used? Edit: And what view is being called when you click "Whatever"? – Paulo Almeida Mar 12 '18 at 19:10
  • I'll edit right now. When I click "Whatever" I use the view "user_panel" which just loads the template "user_panel.html". Guess that could be a problem? – CatalinC Mar 12 '18 at 19:17
  • Well, the problem shouldn't be the template, because it is being successfully used when you render it from the login view. How do you render the template in the `user_panel` view? – Paulo Almeida Mar 12 '18 at 19:23
  • Just edited, in user_panel I just return an HttpResponse. – CatalinC Mar 12 '18 at 19:25
  • Ok, that's it then. Use `return render(request, 'main/user_panel.html')`, like in the other view. To get the user into the context you need the request, because that's where the user comes from (`request.user`). – Paulo Almeida Mar 12 '18 at 19:29
  • Oh, thanks so much, that worked! But now if I click on "Whatever" or the username it downloads a file everytime, nothing specific, just a kind of blank file named "download". I'll post a pic in a moment – CatalinC Mar 12 '18 at 19:35
  • No problem, I actually learned a bit from this too. The download thing is odd, but I don't know what it might be. Try adding spaces after and before the `%` symbols in the `href` for the link. – Paulo Almeida Mar 12 '18 at 19:47
  • I edited that part out of my last comment, because it turns out [you can actually have `div`s inside links in HTML5](https://stackoverflow.com/questions/1827965/is-putting-a-div-inside-an-anchor-ever-correct). Did you also add the spaces in the `href`? In any case, this is now a whole different question... – Paulo Almeida Mar 12 '18 at 19:55
  • Ops, I found out why. Small mistake, instead of render in the user_panel view, there was still HttpResponse, I hit ctrl-z there by accident and boom :). Happens sometimes, then I search for bugs like a madman. Thank you for your time – CatalinC Mar 12 '18 at 20:30
  • You're welcome, I'm glad you got it working. – Paulo Almeida Mar 12 '18 at 20:58

2 Answers2

0

The username is displayed in "user_panel.html" with "{{ user.get_username }}".

It seems you forget to pass user to your template:

return render(request, 'main/user_panel.html', {'user':user})
albar
  • 3,020
  • 1
  • 14
  • 27
0

Just Replace user.get_username with :

request.user.username


or

request.user.get_username

pradip yadav
  • 133
  • 9