0

I'm trying to access the csrf_token inside the django view function.

I have tried importing the csrf:

from django.template.context_processors import csrf

and using it like this

landingPageHtml = landingPageHtml.replace('csrf_token', csrf(request)['csrf_token'])

But I'm getting an error. second argument must be a str not lazy object.

How can I access the csrf token in a view?

@login_required
def viewLandingPage(request, slug):

    lp = getLandingPage(request, slug)

    landingPageHtml = getLandingPageFile(request, lp['file'])

    landingPageHtml = landingPageHtml.replace(
        '{{ lp.seo_title }}', lp['seo_title'])

    landingPageHtml = landingPageHtml.replace(
        '{{ lp.seo_description }}', lp['seo_description'])

    landingPageHtml = landingPageHtml.replace('csrf_token', 'csrf_token')

    return HttpResponse(landingPageHtml)
Xaarth
  • 1,021
  • 3
  • 12
  • 34
  • Does this answer your question? [How can i get csrftoken in view?](https://stackoverflow.com/questions/36347512/how-can-i-get-csrftoken-in-view) – Abdul Aziz Barkat Jan 23 '21 at 09:42
  • 1
    Does this answer your question? [How can i get csrftoken in view?](https://stackoverflow.com/questions/36347512/how-can-i-get-csrftoken-in-view) – Ruli Jan 23 '21 at 13:27

1 Answers1

0

Maybe you are looking for this to get csrf token

django.middleware.csrf.get_token(request)

Reference: How can i get csrftoken in view?

Misster Hao
  • 92
  • 1
  • 1
  • 10