Take database data by session with Django

Asked

Viewed 203 times

0

Start my session

request.session['username'] = 'usuario'
request.session['password'] = 'senha'

Here is the following, if there is a session username, password and there is no session Challenge, i would like you to take the database data by the username that is open. (my case the session name is user same)

def gender(request):
  if request.session.get('username') and request.session.get('password') and not request.session.get('challenge'):
    a = User.objects.all()
    return render(request, 'gender/gender.html', {'a': a})
  return redirect('home')

Summarizing the code above, if gender equals = 1 redirects to home if it is 0 choice.

But you’re getting all the data from the database, I’d just like my session

1 answer

0


I decided with the following:

With the session opened:

request.session['username'] = 'Guilherme'
request.session['password'] = '1234567'

I make contact with my model, I ask him to get everything from the name of the session, in this case, Guilherme:

getAllData = User.objects.get(username=request.session['username'])

With that, I just check:

if getAllData.gender == 1 or getAllData.gender == 2:
   return redirect('home')

If mine gender for 1 or 2 it redirects to home.

That’s all.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.