如何在 django 视图中访问 c​​srf_token

问题描述

我正在尝试访问 django 视图函数中的 csrf_token。

我尝试导入 csrf:

from django.template.context_processors import csrf

并像这样使用它

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

但是我遇到了一个错误。第二个参数必须是 str 而不是惰性对象。

如何在视图中访问 c​​srf 令牌?

@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)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)