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:
This part is fine, I just hit Log In and it works.
Then I get here:
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:
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: