0

Django Session related question Question

My code main template: {% if 'userid' in request.session %} <a href="{% url 'homepage:writearticle' %}" id="artiplus"> <i class="fa fa-plus"></i> Article </a> <a href="#" title="user" style="color:tomato;"><i class="fa fa-user"></i> <span class="big">{{uname}}</span></a> <a href="{% url 'homepage:logout' %}" title="logout"><span class="big">Log Out</span> <i class="fa fa-sign-out"></i></a> {% else %} My views.py: def login(request): # do some validation request.session['userid'] = duname[0].pk sessionID = request.session['userid'] uname = UserModel.objects.get(pk=sessionID).uname return render(request, "homepage/index.html", {"sessionID": sessionID, "uname": uname}) so my problem is when i logged in, the data in "uname" gets displayed once BUT whenever i go to some other pages in my project the data in "uname" gets lost. Pliz Help Me.

28th Aug 2021, 3:47 PM
Pukhrambam
Pukhrambam - avatar
8 Antworten
+ 1
Pukhrambam try this beforehand. {{ request.user.uname }}
29th Aug 2021, 11:38 AM
Obloev Komronbek
Obloev Komronbek - avatar
+ 1
My first question: are you using customised user model? If yes, then what type of customisation you have made? I mean pure re-constructing model or something else
30th Aug 2021, 3:23 PM
AKSHAY🇮🇳
AKSHAY🇮🇳 - avatar
+ 1
AKSHAY🇮🇳 yes it's customised
31st Aug 2021, 4:58 AM
Pukhrambam
Pukhrambam - avatar
+ 1
AKSHAY🇮🇳 it's completely new Model
31st Aug 2021, 4:58 AM
Pukhrambam
Pukhrambam - avatar
0
use templatetags
29th Aug 2021, 4:32 AM
Obloev Komronbek
Obloev Komronbek - avatar
0
Obloev Komronbek for example
29th Aug 2021, 8:55 AM
Pukhrambam
Pukhrambam - avatar
0
create templatetag: from django import template register = template.Library() @register.simple_tag def get_uname(request): sessionID = request.session['userid'] uname = UserModel.objects.get(pk=sessionID).uname return uname and {{ uname }} > {% get_uname request %}
29th Aug 2021, 11:46 AM
Obloev Komronbek
Obloev Komronbek - avatar
0
Pukhrambam Then I need to see the list of session variables, can you post it. If you don't know how to get all session variables then do this: in your home view function (view function that you use to display home page) type ''' print (request.session) ''' and then open the homepage and then see the console/command-prompt window, you will see a dictionary is printed. And you have to share that dictionary.
31st Aug 2021, 5:08 AM
AKSHAY🇮🇳
AKSHAY🇮🇳 - avatar