How to use Django {% csrf_token %} in bootstrap popver

旧街凉风 提交于 2021-02-08 12:12:06

问题


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>' 
    });

Thank you all in advance

Edit:

Now I've solved the issue with csrf_token but unable to solve the issue with getting absolute url.


回答1:


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.

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' />

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 }}" >


来源:https://stackoverflow.com/questions/17476821/how-to-use-django-csrf-token-in-bootstrap-popver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!