如何在Bootstrap Popver中使用Django {%csrf_token%} [英] How to use Django {% csrf_token %} in bootstrap popver
问题描述
我正在使用bootstrap popover弹出一个小表格.如何获取{%csrf_token%}以便在JavaScript弹出式窗口中工作.
I am using bootstrap popover to pop a small form. how do I get the {% csrf_token %} to work inside the javascript popover.
$('.delete_btn').popover({
html: 'true', placement: 'top', title: 'Are you sure?',
content: '<form action="{{object.get_delete_url}}" method="post">
{% csrf_token %}<div><input type="hidden" name="next" value="" />
<input type="submit" class="btn btn-danger" value="Confirm" />
</div></form>'
});
谢谢大家
现在,我已经使用csrf_token解决了该问题,但无法通过获取绝对URL来解决该问题.
Now I've solved the issue with csrf_token but unable to solve the issue with getting absolute url.
推荐答案
几天前,我自己遇到了一个相同的问题.您说找到了答案,但我想我也可以将它发布在这里,以供其他可能偶然发现此问题的人使用.
I ran across the same problem myself a few days ago. You said that you found the answer, but I figured I might as well post it here for any others who might stumble across this problem.
当django将csrf令牌标记放入html中时,它将创建一个不可见的html标记.我遇到的问题是,它用单引号引起来:
When django puts the csrf token tag into html, it creates an invisible html tag. The problem I ran across is, it does so with single quotes:
<input type='hidden' name='csrfmiddlewaretoken' value='sometokeninhere' />
这对我来说有点奇怪,因为双引号是标准的,我目前正在调查这些单引号背后的原因.我遇到的另一个问题是,我放入弹出窗口的django变量(即 {{form.variable1}}
)被呈现为带有双引号的输入标签.失败.因此,我使用的解决方法是从上下文中获取csrf令牌变量,并使用它来进行自己的隐藏输入,将 {%csrf_token%}
替换为:
This is a bit strange to me, because double quotes are standard, and I am currently investigating the reasoning behind these single quotes. Another problem I ran across is that django variables (i.e. {{ form.variable1 }}
) that I was putting into the popover were being rendered as input tags with double quotes. Fail. So, the fix that I used is to grab the csrf token variable from the context and using it to make my own hidden input, replacing the {% csrf_token %}
with:
<input type="hidden" name="csrfmiddlewaretoken" value="{{ csrf_token }}" >
这篇关于如何在Bootstrap Popver中使用Django {%csrf_token%}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!