0

I am unable to figure out a way to use the value corresponding to a cookie that I already set in my views.py file

        if user.is_active:
            login(request, user)
            login_dict = {
                'user': user,
            }
            max_age = 60 * 60
            response = render(request, 'index.html', login_dict)
            response.set_cookie('logged_in', 'request.POST['email']', max_age)
            return response

I have to now use the value corresponding to logged_in cookie-key inside my template index.html.

{% if request.COOKIES (<--*This is where I am facing the problem*)%}
        <li>
          <a href="#" class="dropdown-toggle" data-toggle="dropdown"> {{ request.COOKIES.logged_in }} <b class="caret"></b></a>
          <ul class="dropdown-menu">
            <li><a href="#">Settings</a></li>
            <li><a href="#">Preferences</a></li>
            <li class="divider"></li>
            <li><a href="/logout/">LogOut</a></li>
          </ul>
        </li>
        {% else %}
        <li>
            <a href="/login/">Login</a>
        </li>
        <li>
            <a href="/register/">Register</a>
        </li>
        {% endif %}
darkdefender27
  • 396
  • 1
  • 4
  • 15
  • It makes absolutely no sense to explicitly set a "logged_in" cookie. The auth framework already keeps track of logged-in status via a cookie. – Daniel Roseman Jun 08 '14 at 17:51
  • @DanielRoseman I agree, but my doubt is that I need my webapp to provide a persistent login wherein a user once logged-in can move around on the website (privileged area) and can only end his/her session by logging-out. In my case, once I log_in I am redirected to the index page and now if I reload the page, the session/cookie is deleted. – darkdefender27 Jun 09 '14 at 19:08
  • But that is *precisely* what the Django auth framework does: a persistent login that remains until you log out; and it only redirects you to the main page if you ask it to. If you are seeing something different, then you have something wrongly configured; we can help you if you show us your code (in a new question). – Daniel Roseman Jun 09 '14 at 19:11
  • @DanielRoseman Figured out what wrong I was doing. A silly one indeed. I just had to pass 'request.user' on redirected pages from different views in views.py to see the user's session. Thanks for the help though, much appreciated. Referred this, too. http://stackoverflow.com/questions/17621575/django-login-user-and-refresh-on-same-page-without-defining-a-template?rq=1 – darkdefender27 Jun 09 '14 at 20:16

1 Answers1

0

it's better to create a variable and pass it into the template:

if user.is_active:
    login(request, user)
    login_dict = {
        'user': user,
        'your_desired_value': desired_value, # your new variable
    }
    max_age = 60 * 60
    response = render(request, 'index.html', login_dict)
    response.set_cookie('logged_in', request.POST['email'], max_age)
    return response

and in the template:

{% if your_desired_value %}
     {# do magic #}
{% endif %}

also you don't need to pass user variable explicitly, it's always accessible as request.user

yedpodtrzitko
  • 9,035
  • 2
  • 40
  • 42