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)
Maybe you are looking for this to get csrf token
django.middleware.csrf.get_token(request)
Reference: How can i get csrftoken in view?