1π
I donβt know any online examples showing just that, but I do have a personal project that includes that functionality; agconti:stamped.
The entire repo is linked above if you want to clone it and test it out, but the code pertinent to your needs would be;
Ajax post:
var send_data = { 'name': place.name, 'address': address};
var csrftoken = $.cookie('csrftoken');
function csrfSafeMethod(method) {
// these HTTP methods do not require CSRF protection
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}
$.ajaxSetup({
crossDomain: false, // obviates need for sameOrigin test
beforeSend: function(xhr, settings) {
if (!csrfSafeMethod(settings.type)) {
xhr.setRequestHeader("X-CSRFToken", csrftoken);
xhr.setRequestHeader("X-Requested-With","XMLHttpRequest");
}
}
});
$.ajax({ url: '/results/',
type: 'POST',
data: send_data,
success: function(response, status, jqXHR) {
$("#results").html(response);
//console.log('success function resp');
//console.log(jqXHR.getAllResponseHeaders());
},
error: function(obj, status, err) { alert(err); console.log(err); }
});
An example of controlling for injecting a template and normal page viewing:
{% extends x|yesno:"stamped/blank.html,stamped/home.html" %}
{% load stamped_custom_tags %}
{% block results %}
<!-- Your HTML Here -->
<h1> title and stuff </h1>
<div> I contain things! </div>
{% endblock %}
home.html
is my index.html
that contains the #results
div!
Blank.html
:
{% block results %}{% endblock %}
<!-- to allow for corrected shared rendering
with ajax posts and normal django rendering -->
Any normal django view that renders a page will return html that that ajax post above will inject into the #results
div ( not included in the examples above ) on the page the ajax post initiated from.
Edit from your comment:
Iβm assuming that what you really want to display in the preview is something important, like a title to an article. I would have a separate view for previews that renders a previews template. In that template I might do something like:
<!-- blank because this will be injected into your page -->
{{ article_title|truncatechars:9 }}
You could then inject this into your page and display it on mouse over for example.