问题
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