I am new to Django, so five me for any beginner mistakes. I am experiencing an issue where the session data is being lost when a user tries to navigate on my site after logging in. I am making this website on W3Schools (I know it's not a great resource already), and have attached some images of my code home html code and some of my views. Specifically, I will log a user in successfully and the user is sent back to the home page with their session info intact, but as soon as I press the home button on the navbar or any other implemented buttons the session info is lost. In the database, the session info is still there, so it has not been deleted, just lost. Thanks for the help!
home html file:
<body>
<!-- Navbar -->
<div class="topnav">
<div class="topnav">
<a href="{% url 'home' %}" class="topnav" title="Home"><i class="fa fa-home w3-margin-right"></i>WU Leaders</a>
<a href="#" class="topnav" title="Directory"><i class="fa fa-globe"></i></a>
<a href="{% url 'profile' %}" class="topnav" title="Account"><i class="fa fa-user"></i></a>
</div>
</div>
</body>
home view:
def home_view(request):
user = request.user
if user.is_authenticated:
member = Member.objects.get(user = user)
posts_dict = {}
posts_dates = []
for club in member.clublist:
for post in club.postlist:
posts_dict[post.posted_at] = post
posts_dates.append(post.posted_at)
posts_dates = sorted(posts_dates, reverse = True)
return render(request, 'home.html', {'user': user, 'member': member, 'posts_dict': posts_dict, 'posts_dates': posts_dates})
else:
return render(request, "home.html", {})
login view:
def login_view(request):
if request.method == "POST":
form = LoginForm(request.POST)
if form.is_valid():
email = form.cleaned_data['email']
password = form.cleaned_data['password']
user = authenticate(request, username = email, password = password)
if user is not None:
login(request, user)
member = Member.objects.get(user = user)
posts_dict = {}
posts_dates = []
for club in member.clublist:
for post in club.postlist:
posts_dict[post.posted_at] = post
posts_dates.append(post.posted_at)
posts_dates = sorted(posts_dates, reverse = True)
return render(request, 'home.html', {'user': user, 'member': member, 'posts_dict': posts_dict, 'posts_dates': posts_dates})
else:
return render(request, 'auth/login.html', {'form': form, 'errors': form.errors})
else:
form = LoginForm()
return render(request, 'auth/login.html', {'form': form})
profile view:
def profile_view(request):
user = request.user
if user.is_authenticated:
member = Member.objects.get(user = user)
return render(request, 'edit-profile.html', {'user': user, 'member': member})
else:
return render(request, "home.html", {})
I logged some messages to confirm that the session data was lost after being sent to the URLs 'profile' and 'home' from the home page. I do not mess with session data anywhere other than logging the user in.