2

views.py

from django.core.context_processors import csrf
context.update(csrf(request))
{'csrf_token': <django.utils.functional.__proxy__ object at 0xae0f4ec>}

I am trying to add csrf token to my forms.i m generating csrf token in the views like above.But csrf_token value gives some proxy object like the one shown above instead of string.I m using django 1.3.Thanks in advance for any sort of help.

2
  • Why are you doing this manually? Aren't you using RequestContext and {%csrf_token%} in template.
    – Rohan
    Aug 3, 2012 at 9:48
  • @Rohan i m not using RequestContext but i m using {%csrf_token%} in template
    – shiva
    Aug 3, 2012 at 9:50

1 Answer 1

6

The csrf method is lazy, it returns a Promise/Proxy. Once this object is called in the template, the template engine will unicode this object which triggers the real method.

Try this:

print unicode(csrf(request)['csrf_token'])
4
  • @Willain yes now i get the string.but in template i don't have hidden field eventhough i m using {%csrf_token%} template tag.
    – shiva
    Aug 3, 2012 at 10:00
  • is CsrfViewMiddleware in your middleware? docs.djangoproject.com/en/dev/ref/contrib/csrf/#how-to-use-it
    – Willian
    Aug 3, 2012 at 10:02
  • @Willain yes i do have django.middleware.csrf.CsrfViewMiddleware
    – shiva
    Aug 3, 2012 at 10:05
  • What django version are you using?
    – Willian
    Aug 3, 2012 at 10:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.