Reputation: 3560
I need to pass session id in URL query string after login using Django and Python but in my case I am getting some error. My code is below.
def loginsave(request):
"""This function helps to login the user """
if request.method == 'POST':
password = request.POST.get('pass')
uname = request.POST.get('uname')
per = User.objects.all().filter(
Q(password__icontains=password) & Q(uname__icontains=uname)).count()
if per > 0:
user = User.objects.filter(
Q(password__icontains=password) & Q(uname__icontains=uname))
for use in user:
uid = use.id
user_name = use.uname
request.session['id'] = uid
request.session['sess'] = dict(dt=str(datetime.now()),
value='session')
request.session['sess_id'] = 'abcd1234'
return render(request, 'bookingservice/home.html',
{'count': per, 'username': user_name})
else:
return render(request, 'bookingservice/login.html', {})
This is my login function here I am creating session id and I need to pass it over URL. My menu list is given below.
<a href="{% url 'home' %}?token={{request.session["sess_id"]}}">Home</a>
<a href="{% url 'booking' %}">Add Booking</a>
<a href="{% url 'personal' %}">Add Personal Info</a>
I am doing like this but here I am getting the following error.
Exception Value:
Could not parse the remainder: '["sess_id"]' from 'request.session["sess_id"]'
Here I need after login the session id should come over every page URL.
Upvotes: 1
Views: 129
Reputation: 2721
Change
{{request.session["sess_id"]}}
to
{{ request.session.sess_id }}
usually the template language of django works this way. Here a dot in a variable name signifies a lookup.When the template system encounters a dot in a variable name, it tries the following lookups, in this order:
Dictionary lookup. Example: request.session["bar"]
Attribute lookup. Example: request.session.bar
List-index lookup. Example: request.session[bar]
You can find more at docs
Upvotes: 1
Reputation: 631
You have an error in template, it should read (mind the quotes):
<a href="{% url 'home' %}?token={{request.session['sess_id']}}">Home</a>
<a href="{% url 'booking' %}">Add Booking</a>
<a href="{% url 'personal' %}">Add Personal Info</a>
Upvotes: 0