Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8024161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:46:56+00:00 2026-06-04T22:46:56+00:00

Question Using set_test_cookie in a django view fails to actually set a test cookie

  • 0

Question

Using set_test_cookie in a django view fails to actually set a test cookie and I can’t figure out why.

Steps to Reproduce

The actual project is much more complex than this, however, the error is reproducible in a near empty project.

Shell

$ virtualenv venv
$ source venv/bin/activate
(venv)$ pip install django
(venv)$ django-admin.py startproject hellodjango .
(venv)$ python manage.py startapp polls

settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'database.db',                      # Or path to database file if using sqlite3.
        'USER': '',                      # Not used with sqlite3.
        'PASSWORD': '',                  # Not used with sqlite3.
        'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
    }
}

urls.py

from django.conf.urls import patterns, include, url

urlpatterns = patterns('',
    url(r'^login/$', 'hellodjango.polls.views.login'),
)    

polls/views.py

from django.shortcuts import render_to_response
from django.http import HttpResponse

def login(request):
    if request.method == 'POST':
        if request.session.test_cookie_worked():
            request.session.delete_test_cookie()
            return HttpResponse("You're logged in.")
        else:
            return HttpResponse("Please enable cookies and try again.")
    request.session.set_test_cookie()
    return HttpResponse("Look ma, no cookies!")

Shell

(venv)$python manage.py runserver

Browser

Go to localhost:8000/login/ and notice that your get request does not show a response cookie with the name testcookie and the value worked as per the set_test_cookie source.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T22:46:58+00:00Added an answer on June 4, 2026 at 10:46 pm

    Actually, it works! You might want to visit your website with cookies disabled and see for yourself.

    Anyway, here’s the explanation:

    If you look at the source for set_test_cookie, you’ll notice that it actually sets a variable in the current session, not a cookie or anything (at least, not directly).

    Here’s how it works:

    1. You call set_test_cookie, a key of TEST_COOKIE_NAME and of value TEST_COOKIE_VALUE is added to your current session.
    2. You current session was modified (check the __setitem__ method), so a sessionid (1) cookie will be sent (2)
    3. When you POST data in your next request, django checks whether the key TEST_COOKIE_NAME happens to be in your session data and has the right value.

    If it does, it means we were able to retrieve your session from your request, so that the sessionid cookie we used to identify you was sent back (lest we wouldn’t have been able to retrieve your session), so you must support cookies.

    Hope this makes things clearer to you; if it doesn’t, I think you should look into the details of how HTTP works.

    Notes

    (1) Or whatever the SESSION_COOKIE_NAME setting sets.
    (2) Actually, a sessionid cookie will be sent even if the session was not altered if the SESSION_SAVE_EVERY_REQUEST setting is true

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a question about using GalleryView. At first, i set five default images
Quick question I have a database with snapshot enabled using ALTER DATABASE myDB SET
Okay, I have a simple test server set up using socket.io in node.js. My
Following on from this question , I'm using a simple ViewModel class to test
I'm using the jQuery Cycle plugin. The page in question is here: http://harrisonfjord.com/folio/test.html The
This question is more Python related than Django related. I want to test write
I am using the open_graph Explorer tool to test this out and I need
Question: Using Ruby it is simple to add custom methods to existing classes, but
I recently posted a question using a lambda function and in a reply someone
Just a quick question about using select() . I'm using select() to read from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.