---
title: "Django 测试cookies是否可用"
author: "华天下"
date: "2011-03-13T19:08:45Z"
tags: []
categories: ["编程语言"]
slug: "django-cookies"
draft: false
lastmod: "2011-03-13T19:09:03Z"
id: 960502
url: "https://czh.iteye.com/blog/960502"
---

```python
def login(request):

    # If we submitted the form...
    if request.method == 'POST':

        # Check that the test cookie worked (we set it below):
        if request.session.test_cookie_worked():

            # The test cookie worked, so delete it.
            request.session.delete_test_cookie()

            # In practice, we'd need some logic to check username/password
            # here, but since this is an example...
            return HttpResponse("You're logged in.")

        # The test cookie failed, so display an error message. If this
        # were a real site, we'd want to display a friendlier message.
        else:
            return HttpResponse("Please enable cookies and try again.")

    # If we didn't post, send the test cookie along with the login form.
    request.session.set_test_cookie()
    return render_to_response('foo/login_form.html')

```